利用SOAP(Webservice)上傳文件
2024-07-21 02:21:18
供稿:網友
 
本文系鼎鼎原創,如轉載,請注明出處:http://blog.csdn.net/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;
客戶端直接用調用,把數據流作參數傳上來就完了。