利用SOAP(Webservice)上传文件

本文介绍了如何通过SOAP WebService实现文件上传。作者提供了一个`SaveFile`方法,该方法接收字节数组和文件名作为参数,将数据保存到服务器指定路径。如果保存路径不存在,方法会抛出异常。在尝试写入文件后,确保数据成功写入硬盘并返回成功状态。

本文系鼎鼎原创,如转载,请注明出处:http://blog.youkuaiyun.com/weisunding

[WebMethod(Description="上传并保存图片文件")]
  public bool SaveFile(byte[] binData,string fileName){
   bool success=false;   

   string savePath=System.Configuration.ConfigurationSettings.AppSettings["UploadDirectory"];
   if(savePath==null) savePath="Photo";
   if(savePath.IndexOf("://")<0) savePath=Server.MapPath(savePath);//不是绝对路径
   if(!savePath.EndsWith("//")) savePath += "//";

   if(!Directory.Exists(savePath)){
    throw new Exception("服务器端没有找到有效的保存路径!");
   }
   
   FileStream fileStream=null;
   try{
    fileStream=new FileStream(savePath + fileName,FileMode.Create,FileAccess.Write);
    //write the file
    fileStream.Write(binData,0,binData.Length);
    fileStream.Flush();//clear the buffer,write the data to the hard disk
    success=true;
   }catch(Exception ex){
    throw new Exception(ex.Message); 
   }finally{
    fileStream.Close();
   }
   return success;
   
  }  

解决思路:编写WebService过程SaveFile(byte[] binData,string fileName0;
客户端直接用调用,把数据流作参数传上来就完了。

 

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值