HTTP协议下用Web Service上传大文件的解决方案

一片金黄的阳光,照着苍绿的崖壁,崖壁上长着漂亮的小花,像蝴蝶一样动人。走入自然,踏青游玩,在大自然中尽情放松,享受清新空气。
用HTTP协议上传大文件也许是个不好办的问题。主要是它的不连续性,使得上传文件感觉很“危险”。特别是很大的文件(几百MB甚至是上G的文件),心里总觉得不踏实,一不小心就会出现问题,而一但出现问题就无法继续上传,这是很郁闷的。 后来在一些网站上找到一些上传文件的组件,但都是要用到一些COM组件。至于后来的ASP.net下上传大文件的解决方案,我也做过一个组件,后来发现根本就不用自己写什么组件,利用ASP.net自己的上传方法也可以解决大文件上传,真是郁闷的要死了。。。。 回想之后,决定用Webservice来做一个文件上传,还是利用HTTP协议,这样不用在服务器上做太多的变动,而客户端也简单。 首先是解决方案的设计:因为Webservice可以利用SOAP来传递数据,而且可以传递十进制数据,因此可以想到,在服务上公开一个方法,参数可以是byte数组,这样可以把文件分块的上传到服务器。这一解决方法我做过,但速度很慢。后来在MS上找到一些文章,用MS最新公开的服务组件上传文件,速度快了很多。而自己所要做的就是组织一些安全性的问题。 部份代码:UploadInstance

usingSystem;
usingSystem.IO;
usingMicrosoft.Web.Services2;
usingMicrosoft.Web.Services2.Dime; namespaceWebb.WAVE.WinUpload
{
/**////<summary>
///SummarydescriptionforControls.
///</summary>
publicclassUploadInstance2
{
Fields#regionFields
privatestringm_GUID;
privateDateTimem_uploadTime;
privatelongm_fileLength;
privatelongm_currentPoint;
privatestringm_pathOnserver;
privatelongm_userID;
#endregion Properties#regionProperties
publiclongUserID
{
get{returnthis.m_userID;}
set{this.m_userID=value;}
}
publicstringGUID
{
get{returnthis.m_GUID;}
set{this.m_GUID=value;}
}
publicDateTimeUploadTime
{
get{returnthis.m_uploadTime;}
set{}
}
publiclongFileLength
{
get{returnthis.m_fileLength;}
set{this.m_fileLength=value;}
}
publiclongCurrentPoing
{
get{returnthis.m_currentPoint;}
set{this.m_currentPoint=value;}
}
publicstringPathOnServer
{
get{returnthis.m_pathOnserver;}
set{this.m_pathOnserver=value;}
}
publicstringFullPathOnServer
{
get
{
if(this.m_GUID!=string.Empty&&this.m_pathOnserver!=string.Empty)
{
returnPath.Combine(this.m_pathOnserver,this.m_GUID+".rem");
}
else
{
returnstring.Empty;
}
}
}
publicstringFileName
{
get
{
if(this.m_GUID!=string.Empty)
{
returnthis.m_GUID+".rem";
}
else
{
returnstring.Empty;
}
}
} #endregion publicUploadInstance2()
{
this.m_GUID=System.Guid.NewGuid().ToString();
this.m_uploadTime=System.DateTime.Now;
this.m_currentPoint=0;
this.m_fileLength=0;
this.m_pathOnserver=string.Empty;
}
publicUploadInstance2(stringi_path,stringi_GUID,longi_fileLength)
{
stringm_fullPath=Path.Combine(i_path,i_GUID);
if(!File.Exists(m_fullPath))return;
this.m_GUID=i_GUID;
this.m_uploadTime=System.DateTime.Now;
this.m_pathOnserver=i_path;
FileInfom_fileInfo=newFileInfo(m_fullPath);
this.m_currentPoint=m_fileInfo.Length;
this.m_fileLength=i_fileLength;
} publicboolUploadData(byte[]i_data,longi_currentPoint,inti_dataSize)
{
stringm_fullPath=this.FullPathOnServer;
if(!File.Exists(m_fullPath)&&this.m_currentPoint!=0)returnfalse;
longm_filePoint=newFileInfo(m_fullPath).Length;
if(m_filePoint!=i_currentPoint)returnfalse;
FileStreamm_fileStream=newFileStream(m_fullPath,FileMode.Append);
m_fileStream.Write(i_data,0,i_dataSize);
m_fileStream.Close();
returntrue;
} publicvoidAbandantUpload()
{
stringm_fullPath=this.FullPathOnServer;
try{File.Delete(m_fullPath);}
catch{}
} publicvoidCreateFile()
{
stringm_fullPath=this.FullPathOnServer;
if(!File.Exists(m_fullPath))
{
File.Create(m_fullPath).Close();
}
else
{
try
{
File.Delete(m_fullPath);
}catch{}
File.Create(m_fullPath).Close();
}
}
}
}

上传过程:

#regionUploadProcess
publicvoidUploadProcess()
{
DateTimem_start=DateTime.Now;
this.textBox_OutMsg.AppendText("Initializeupload\r\n");
if(this.m_upload==null||this.m_uploadGUID==null||this.m_uploadGUID==string.Empty)
{
this.textBox_OutMsg.AppendText("Uploadinstanceiderrororlogintotheserverfaild\r\n");
this.textBox_OutMsg.AppendText("Uploadfaild.\r\n");
return;
}
this.textBox_OutMsg.AppendText("Openfile\r\n");
if(this.m_filePath==null||this.m_filePath==string.Empty||!File.Exists(this.m_filePath))
{
this.textBox_OutMsg.AppendText("Openfileerror\r\n");
this.textBox_OutMsg.AppendText("Uploadfaild.\r\n");
return;
}
FileInfom_fileInfo=newFileInfo(this.m_filePath);
FileStreamm_fs=newFileStream(this.m_filePath,FileMode.Open,FileAccess.Read);
this.textBox_OutMsg.AppendText("Startuploadfile\r\n");
intm_buffer=10;//KBytes
longm_currentPoint=0;
longm_fileLength=m_fileInfo.Length;
boolm_uploadResult=false;
byte[]m_data=newbyte[m_buffer*1024];
longm_readBytes=m_fs.Read(m_data,0,m_buffer*1024);
this.UploadProcessBar.Maximum=100;
this.UploadProcessBar.Minimum=0;
while(m_readBytes>0)
{
MemoryStreamm_memoryStream=newMemoryStream(m_data,0,(int)m_readBytes);
DimeAttachmentdimeAttach=newDimeAttachment("image/gif",TypeFormat.MediaType,m_memoryStream);
this.m_upload.RequestSoapContext.Attachments.Add(dimeAttach);
m_uploadResult=this.m_upload.UploadFileData(this.m_uploadInstance,m_currentPoint,m_readBytes);
if(m_uploadResult)
{
m_currentPoint+=m_readBytes;
m_readBytes=m_fs.Read(m_data,0,m_buffer*1024);
//this.textBox_OutMsg.AppendText("Uploading:"+m_currentPoint.ToString()+"/"+m_fileLength.ToString()+"\r\n");
this.UploadProcessBar.Value=(int)(m_currentPoint*100/m_fileLength);
this.label_outPercent.Text=this.UploadProcessBar.Value.ToString()+"%";
}
else
{
this.textBox_OutMsg.AppendText("Uploadfileerror.\r\n");
m_fs.Close();
this.m_upload.AbandantUpload(this.m_uploadInstance);
return;
}
}
this.textBox_OutMsg.AppendText("Fileuploadfinished.\r\n");
this.button_Cancel.Enabled=false;
m_fs.Close();
this.ResetForm();
}
#endregion
测试项目代码:
http://test.0579fw.com/myfile/kiyeer/客户上传/webbwinupload.zip 出现错误的解决方法:
***************************************************** 引用内容
Error1'WinFormTest.localhost.WebbWinUpload'doesnotcontainadefinitionfor'RequestSoapContext'D:\WebbWinUpload\WinFormTest\WebbWinUpload.cs44819WinFormTest 当你更新Web引用的时候,.net自动生成的Web引用为:
publicclassWebbWinUpload:System.Web.Services.Protocols.SoapHttpClientProtocol
请转化为:
publicclassWebbWinUpload:Microsoft.Web.Services2.WebServicesClientProtocol
查找引用下自动生成的C#文件Reference.cs

本文HTTP协议下用Web Service上传大文件的解决方案到此结束。年轻时代是培养习惯、希望及信仰的一段时光。小编再次感谢大家对我们的支持!

标签: 大文件 HTTP