c# webservice上传下载

这篇博客介绍了如何使用C#实现WebService进行文件上传和下载。提供了上传文件的WebMethod,通过接收文件的字节数组、路径和文件名,将文件保存到服务器。同时,还展示了下载文件的方法,根据提供的文件相对路径,返回文件的字节数组。客户端代码中包含上传和下载的示例,包括将本地文件转换为字节数组,调用WebService接口,并保存下载的文件到本地。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WebService代码:

///
    ///上传文件
    ///
    ///文件的byte[]
    ///上传文件的路径
    ///上传文件名字
    ///
   [WebMethod]
    public boolUploadFile(byte[] fs, string path, string fileName)
    {
       bool flag = false;
       try
       {
           //获取上传案例图片路径
           path = Server.MapPath(path);
           if (!Directory.Exists(path))
           {
               Directory.CreateDirectory(path);
           }
           //定义并实例化一个内存流,以存放提交上来的字节数组。
           MemoryStream m = new MemoryStream(fs);
           //定义实际文件对象,保存上载的文件。
           FileStream f = new FileStream(path + "\" + fileName,FileMode.Create);
           //把内内存里的数据写入物理文件
           m.WriteTo(f);
           m.Close();
           f.Close();
           f = null;
           m = null;
           flag = true;
       }
       catch (Exception ex)
       {
           flag = false;
       }
       return flag;
    }

   [WebMethod(Description = "下载服务器站点文件,传递文件相对路径")]
    publicbyte[] DownloadFile(string strFilePath, string path)
    {
       FileStream fs = null;
       string CurrentUploadFolderPath =HttpContext.Current.Server.MapPath(path);

       string CurrentUploadFilePath = CurrentUploadFolderPath + "\" +strFilePath;
       if (File.Exists(CurrentUploadFilePath))
       {
           try
           {
               ///打开现有文件以进行读取。
               fs = File.OpenRead(CurrentUploadFilePath);
               int b1;
               System.IO.MemoryStream tempStream = newSystem.IO.MemoryStream();
               while ((b1 = fs.ReadByte()) != -1)
               {
                   tempStream.WriteByte(((byte)b1));
               }
               return tempStream.ToArray();
           }
           catch (Exception ex)
           {
               return new byte[0];
           }
           finally
           {
               fs.Close();
           }
       }
       else
       {
           return new byte[0];
       }

客户端代码:

 ///
       /// 上传图片附件
       ///
       ///
       private bool UploadImage()
       {
           bool flag = true;
           string path = @"C:\Documents and Settings\Administrator\MyDocuments\My Pictures\10121312156cf4a761c504fe69.jpg";//本地路径
           byte[] bytes = GetBytesByPath(path);//获取文件byte[]
           string uploadPath = "image";//上传服务器文件夹路径
           string fileName = "img18.jpg";//文件名称
           try
           {
               localhost.Service s = newWindowsFormsApplication1.localhost.Service();
               if (s.UploadFile(bytes, uploadPath, fileName)) { flag = true;}
               else { flag = false; }
           }
           catch
           {
               flag = false;
           }
           return flag;
       }
       ///
       /// 根据文件的路径获取图片的byte[]
       ///
       ///
       ///
       public static byte[] GetBytesByPath(string path)
       {
           FileStream fs = new FileStream(path, FileMode.Open,FileAccess.Read);
           BinaryReader br = new BinaryReader(fs);
           byte[] bytes = br.ReadBytes((int)fs.Length);
           fs.Flush();
           fs.Close();
           return bytes;
       }

       private void DownFile()
       {
           localhost.Service s = newWindowsFormsApplication1.localhost.Service();
           byte[] bs = s.DownloadFile("img18.jpg","image");
           FileStream stream=new FileStream(@"C:\Documents andSettings\Administrator\My Documents\My Pictures\18.jpg",FileMode.CreateNew);
           stream.Write(bs,0,bs.Length);
           stream.Flush();
           stream.Close();
       }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值