Solution for uploading large files using Web services over HTTP

Source: Internet
Author: User

Uploading large files over HTTP may be difficult. This is mainly because of its non-continuity, which makes it very dangerous to upload files ". Especially for large files (hundreds of MB or even GB files), I always feel that I am not steadfast, and problems may occur if I am not careful, however, when a problem occurs, the upload cannot be continued, which is depressing.

Later, I found some File Upload components on some websites, but they all needed some COM components. As for the solution for uploading large files under ASP.net, I also made a component and later found that I didn't have to write any components myself, using ASP.net's own upload method can also solve the problem of uploading large files, which is really depressing ....

In retrospect, I decided to use Web service for file upload or HTTP protocol, so that there is no need to make too many changes on the server, and the client is also simple.

The first is the design of the solution: Because the Web service can use SOAP to transmit data and transmit decimal data, you can think of a method published on the service. The parameter can be a byte array, this allows you to upload files to the server in multiple parts. I have done this solution but it is slow. Later, I found some articles on MS and used MS's latest public service component to upload files, which is much faster. All you have to do is organize some security issues.

Part of the Code: Upload Instance

Copy codeThe Code is 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 codeThe Code is 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 x 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. uploadprocw.ar. 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://test0000579f0000com/myfile/kiyeer/customer uploads/webbwinupload.zip

Solution to the error:
**************************************** *************

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

When you update a Web reference, the Web reference automatically generated by. net is:
Public class WebbWinUpload: System. Web. Services. Protocols. SoapHttpClientProtocol
Convert:
Public class WebbWinUpload: Microsoft. Web. Services2.WebServicesClientProtocol
Find the automatically generated C # file Reference. cs under 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.