The solution of uploading large files with Web service under HTTP protocol _ practical skills

Source: Internet
Author: User
Tags datetime
Uploading large files with the HTTP protocol may be a difficult problem to run. Mainly because of its discontinuity, making uploading files feel very "dangerous". In particular, a large file (hundreds of MB or even on the G file), the mind always feel not practical, inadvertently will appear problems, and a problem can not continue to upload, it is very depressing.

Later on some sites to find some of the upload file components, but are to use some COM components. As for the later ASP.net upload large file solution, I have also done a component, and then found that there is no need to write their own components, using asp.net own upload method can also solve large file upload, really depressed to die ....

After recalling, decide to use the Web service to do a file upload, or use the HTTP protocol, so do not have to do too many changes on the server, and the client is simple.

The first is the design of the solution: because the Web service can use SOAP to pass data, and it can pass decimal data, it can be thought of exposing a method on a service that can be a byte array so that the file can be uploaded to the server in chunks. I've done this solution, but it's slow. Later on MS to find some articles, with the latest public service components of Ms upload files, a lot faster. And all you have to do is organize some security issues.

Part code: Upload Instance

Copy Code code as follows:

Using System;
Using System.IO;
Using Microsoft.Web.Services2;
Using Microsoft.Web.Services2.Dime;

Namespace Webb.WAVE.WinUpload
{
/**////<summary>
Summary description for Controls.
</summary>
public class UploadInstance2
{
Fields#region Fields
private string M_guid;
Private DateTime M_uploadtime;
Private long m_filelength;
Private long m_currentpoint;
private string M_pathonserver;
Private long M_userid;
#endregion

Properties#region Properties
Public long UserID
{
Get{return This.m_userid;}
Set{this.m_userid=value;}
}
public string GUID
{
Get{return This.m_guid;}
Set{this.m_guid=value;}
}
Public DateTime Uploadtime
{
Get{return This.m_uploadtime;}
set{}
}
Public long Filelength
{
Get{return This.m_filelength;}
Set{this.m_filelength=value;}
}
Public long currentpoing
{
Get{return This.m_currentpoint;}
Set{this.m_currentpoint=value;}
}
public string Pathonserver
{
Get{return This.m_pathonserver;}
Set{this.m_pathonserver=value;}
}
public string Fullpathonserver
{
Get
{
if (this.m_guid!=string. Empty&&this.m_pathonserver!=string. Empty)
{
Return Path.Combine (this.m_pathonserver,this.m_guid+ ". rem");
}
Else
{
return string. Empty;
}
}
}
public string FileName
{
Get
{
if (this.m_guid!=string. Empty)
{
Return this.m_guid+ ". rem";
}
Else
{
return string. Empty;
}
}
}

#endregion

Public UploadInstance2 ()
{
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;
}
Public UploadInstance2 (String i_path,string i_guid,long i_filelength)
{
String m_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;
FileInfo m_fileinfo = new FileInfo (M_fullpath);
This.m_currentpoint = M_fileinfo.length;
This.m_filelength = I_filelength;
}

public bool Uploaddata (byte[] i_data, long i_currentpoint, int i_datasize)
{
String M_fullpath = this. Fullpathonserver;
if (! File.exists (M_fullpath) &&this.m_currentpoint!=0) return false;
Long m_filepoint = new FileInfo (M_fullpath). Length;
if (M_filepoint!=i_currentpoint) return false;
FileStream M_filestream = new FileStream (m_fullpath,filemode.append);
M_filestream.write (i_data,0,i_datasize);
M_filestream.close ();
return true;
}

public void Abandantupload ()
{
String M_fullpath = this. Fullpathonserver;
Try{file.delete (M_fullpath);}
catch{}
}

public void CreateFile ()
{
String M_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 ();
}
}
}
}

Upload process:
Copy Code code as follows:

#region uploadprocess
public void uploadprocess ()
{
DateTime M_start = DateTime.Now;
This.textBox_OutMsg.AppendText ("Initialize upload\r\n");
if (this.m_upload==null| | this.m_uploadguid==null| | This.m_uploadguid==string. Empty)
{
This.textBox_OutMsg.AppendText ("Upload instance ID error or login to the" server faild\r\n);
This.textBox_OutMsg.AppendText ("Upload faild.\r\n");
Return
}
This.textBox_OutMsg.AppendText ("Open file\r\n");
if (this.m_filepath==null| | This.m_filepath==string. empty| |! File.exists (This.m_filepath))
{
This.textBox_OutMsg.AppendText ("Open file error\r\n");
This.textBox_OutMsg.AppendText ("Upload faild.\r\n");
Return
}
FileInfo m_fileinfo = new FileInfo (This.m_filepath);
FileStream m_fs = new FileStream (This.m_filepath, FileMode.Open, FileAccess.Read);
This.textBox_OutMsg.AppendText ("Start upload file\r\n");
int m_buffer = 10; Kbytes
Long m_currentpoint = 0;
Long m_filelength = m_fileinfo.length;
BOOL M_uploadresult = false;
byte[] m_data = new byte[m_buffer*1024];
Long m_readbytes = M_fs. Read (m_data, 0, m_buffer*1024);
This. Uploadprocessbar.maximum = 100;
This. Uploadprocessbar.minimum = 0;
while (m_readbytes>0)
{
MemoryStream m_memorystream = new MemoryStream (m_data, 0, (int) m_readbytes);
DimeAttachment Dimeattach = new DimeAttachment ("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 ("Upload file error.\r\n");
M_fs. Close ();
This.m_upload. Abandantupload (this.m_uploadinstance);
Return
}
}
This.textBox_OutMsg.AppendText ("File upload finished.\r\n");
this.button_Cancel.Enabled = false;
M_fs. Close ();
This. Resetform ();
}
#endregion


Test Project Code:
http://test.0579fw.com/myfile/kiyeer/Customer Upload/webbwinupload.zip

The workaround for the error occurs:
*****************************************************

Reference content
Error 1 ' WinFormTest.localhost.WebbWinUpload ' does not contain a definition for ' requestsoapcontext ' D:\WebbWinUpload\ Winformtest\webbwinupload.cs 448 Winformtest

When you update a Web reference,. NET-generated web references are:
public class WebbWinUpload:System.Web.Services.Protocols.SoapHttpClientProtocol
Please convert to:
public class WebbWinUpload:Microsoft.Web.Services2.WebServicesClientProtocol
Automatically generated C # file Reference.cs under Lookup reference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.