第二种方案是我查資料弄明白的,這種方法在文件数据不大的时候可以用。但传输大文件时会遇到超时的错误。
C#Hunter告訴我一個更好的方案:
方案一:
对应的解决方法可以参考:Dime Buffered Upload
http://www.codeproject.com/cs/webservices/DimeBufferedUpload.asp 源碼:
Download source (WinForms, Web Service) - 545 Kb Download demo app (WinForms, Web Service) - 493 Kb
方案二 :
Webservice:
public Service1()
{
InitializeComponent();
}
[WebMethod]
public string UploadFile(byte[] fs,string FileName)
{
try
{
MemoryStream m = new MemoryStream(fs);
//FileStream f = new FileStream(Server.MapPath(".") + "\\"+ FileName, FileMode.Create);
FileStream f = new FileStream(@"D:\"+ FileName, FileMode.Create); //這個路徑可以指定的,但是,一定要開放.Net寫的權限
m.WriteTo(f);
m.Close();
f.Close();
f = null;
m = null;
return "success !";
}
catch(Exception ex)
{
return ex.Message;
}
}
調用頁面
try
{
System.Web.HttpFileCollection oFiles;
oFiles = System.Web.HttpContext.Current.Request.Files;
string FilePath = oFiles[0].FileName;
string FileName = FilePath.Substring(FilePath.LastIndexOf("\\")+1);
![](/Images/OutliningIndicators/InBlock.gif)
WebReference.Service1 s1 = new com.foxconn.cr.CostomerRisk.WebReference.Service1();
byte[] b = new byte[oFiles[0].ContentLength];
System.IO.Stream fs;
fs = (System.IO.Stream)oFiles[0].InputStream;
fs.Read(b, 0, oFiles[0].ContentLength);
string tt = s1.UploadFile(b, FileName);
fs.Close();
}
catch(Exception ex)
{
this.L_msg.Visible=true;
this.L_msg.Text = ex.ToString();
}