通过WebService上传任何类型的文件

通过WebService上传任何类型的文件

1.简介

本文描述了一种方法,通过Web服务从Windows窗体应用程序,可用于所有类型的文件上传。证明的方法不依赖于ASP.NET文件上传控件,并允许开发人员有机会上传文件的编程,而无需用户干预。这种做法可能是有用的,做这样的事情处理本地消息队列中的内容时,互联网服务是可用的(如果用户群是移动,只有间歇性的连接)。文章还讨论了为先导,以允许文件上传,通过服务使用的文件大小检查。

2.服务器端WebService编写

首先在VS中创建一个Webservice工程,并在WebService中编写如下方法:

  1. [WebMethod]  
  2. public string UploadFile(byte[] f, string fileName)  
  3. {  
  4.     try  
  5.     {  
  6.         MemoryStream ms = new MemoryStream(f);  
  7.   
  8.         FileStream fs = new FileStream  
  9.             (System.Web.Hosting.HostingEnvironment.MapPath  
  10.             ("~/TransientStorage/") +  
  11.             fileName, FileMode.Create);  
  12.   
  13.         ms.WriteTo(fs);  
  14.   
  15.         ms.Close();  
  16.         fs.Close();  
  17.         fs.Dispose();  
  18.   
  19.         return "OK";  
  20.     }  
  21.     catch (Exception ex)  
  22.     {  
  23.         return ex.Message.ToString();  
  24.     }  
  25. }  
3.客户端窗体程序编写

客户端上传文件的方法编写如下:

  1. private void UploadFile(string filename)  
  2. {  
  3.     try  
  4.     {  
  5.         String strFile = System.IO.Path.GetFileName(filename);  
  6.   
  7.         TestUploader.Uploader.FileUploader srv = new  
  8.         TestUploader.Uploader.FileUploader();  
  9.   
  10.         FileInfo fInfo = new FileInfo(filename);  
  11.   
  12.         long numBytes = fInfo.Length;  
  13.         double dLen = Convert.ToDouble(fInfo.Length / 1000000);  
  14.   
  15.         if (dLen < 4)  // 文件上传有4M限制,超过4M的需要分割发送  
  16.         {  
  17.             FileStream fStream = new FileStream(filename,  
  18.             FileMode.Open, FileAccess.Read);  
  19.             BinaryReader br = new BinaryReader(fStream);  
  20.   
  21.             byte[] data = br.ReadBytes((int)numBytes);  
  22.             br.Close();  
  23.   
  24.             service  
  25.             string sTmp = srv.UploadFile(data, strFile);  
  26.             fStream.Close();  
  27.             fStream.Dispose();  
  28.   
  29.             message  
  30.             MessageBox.Show("File Upload Status: " + sTmp, "File  
  31.             Upload");  
  32.         }  
  33.         else  
  34.         {  
  35.             MessageBox.Show("The file selected exceeds the size limit  
  36.             for uploads.", "File Size");  
  37.         }  
  38.     }  
  39.     catch (Exception ex)  
  40.     {  
  41.         MessageBox.Show(ex.Message.ToString(), "Upload Error");  
  42.     }  
  43. }  
4.客户端文件上传示例

新建一个窗体工程,并在窗体上拖放两个Button,一个TextBox控件,一个按钮是浏览文件,一个按钮上传文件,TextBox显示文件路径。

1)浏览文件代码

  1. private void btnBrowse_Click(object sender, EventArgs e)  
  2. {  
  3.     openFileDialog1.Title = "打开文件";  
  4.     openFileDialog1.Filter = "所有文件|*.*";  
  5.     openFileDialog1.FileName = "";  
  6.   
  7.     try  
  8.     {  
  9.         openFileDialog1.InitialDirectory = "C:\\Temp";  
  10.     }  
  11.     catch{ }  
  12.   
  13.     openFileDialog1.ShowDialog();  
  14.   
  15.     if (openFileDialog1.FileName == "")  
  16.         return;  
  17.     else  
  18.         txtFileName.Text = openFileDialog1.FileName;  
  19. }  

2)文件上传的代码

  1. private void btnUpload_Click(object sender, EventArgs e)  
  2. {  
  3.     if (txtFileName.Text != string.Empty)  
  4.         UploadFile(txtFileName.Text);  
  5.     else  
  6.         MessageBox.Show("请选择要上传的文件!""没有选择文件");  
  7. }  

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值