HTTP 프로토콜에서 큰 파일을 웹 서비스로 업로드하는 솔루션

8176 단어
HTTP 프로토콜로 큰 파일을 올리는 것은 아마도 어려운 문제일 것이다.주로 그것의 불연속성 때문에 파일을 업로드하는 것이 매우 위험하다고 느낀다.특히 큰 파일(수백MB, 심지어 G에 오른 파일)은 늘 마음이 든든하지 않아 자칫 문제가 생길 수 있는데 문제가 생기면 계속 올릴 수 없어 답답하다.
나중에 일부 사이트에서 파일을 업로드하는 구성 요소를 찾았지만, 모두 COM 구성 요소를 사용해야 했다.이후의 ASP에 대해서는net에서 큰 파일을 업로드하는 해결 방안, 나도 하나의 구성 요소를 만들었는데 나중에 자신이 어떤 구성 요소를 쓰지 않고 ASP를 이용하는 것을 발견했다.net 자체 업로드 방법도 큰 파일 업로드를 해결할 수 있어 답답해 죽겠어...
회상 후에 웹 서비스로 파일 업로드를 할지 HTTP 프로토콜을 이용할지 결정하면 서버에 너무 많은 변동을 하지 않고 클라이언트도 간단하다.
우선 해결 방안의 디자인: 웹 서비스는 SOAP를 이용하여 데이터를 전달할 수 있고 십진 데이터를 전달할 수 있기 때문에 서비스에 매개 변수는byte 수조로 공개할 수 있어 파일을 블록으로 나누어 서버에 올릴 수 있다는 방법을 생각해 볼 수 있다.이 해결 방법은 내가 해 보았지만 속도가 매우 느리다.나중에 MS에서 일부 글을 찾아 MS가 최근 공개한 서비스 구성 요소로 파일을 업로드했는데 속도가 훨씬 빨랐다.자신이 해야 할 일은 조직의 안전성 문제다.
부분 코드: Upload Instance
 
  
using System; 
using System.IO; 
using Microsoft.Web.Services2; 
using Microsoft.Web.Services2.Dime; 

namespace Webb.WAVE.WinUpload 

/**////  
/// Summary description for Controls. 
/// 
 
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(); 





업로드 프로세스:
 
  
#region UploadProcess 
public void UploadProcess() 

DateTime m_start = DateTime.Now; 
this.textBox_OutMsg.AppendText("Initialize upload\r
"); 
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
"); 
this.textBox_OutMsg.AppendText("Upload faild.\r
"); 
return; 

this.textBox_OutMsg.AppendText("Open file\r
"); 
if(this.m_filePath==null||this.m_filePath==string.Empty||!File.Exists(this.m_filePath)) 

this.textBox_OutMsg.AppendText("Open file error\r
"); 
this.textBox_OutMsg.AppendText("Upload faild.\r
"); 
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
"); 
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
"); 
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
"); 
m_fs.Close(); 
this.m_upload.AbandantUpload(this.m_uploadInstance); 
return; 


this.textBox_OutMsg.AppendText("File upload finished.\r
"); 
this.button_Cancel.Enabled = false; 
m_fs.Close(); 
this.ResetForm(); 

#endregion 

테스트 항목 코드:
http://test.0579fw.com/myfile/kiyeer/고객 업로드/webbwinupload.zip
오류 해결 방법:
*****************************************************
컨텐트 참조
Error 1 'WinFormTest.localhost.WebbWinUpload' does not contain a definition for 'RequestSoapContext' D:\WebbWinUpload\WinFormTest\WebbWinUpload.cs 448 19 WinFormTest 
웹 참조를 업데이트할 때net에서 자동으로 생성되는 웹 참조는 다음과 같습니다.
public class WebbWinUpload : System.Web.Services.Protocols.SoapHttpClientProtocol 
다음으로 전환하십시오.
public class WebbWinUpload : Microsoft.Web.Services2.WebServicesClientProtocol 
참조에서 자동으로 생성된 C# 파일 Reference를 찾습니다.cs

좋은 웹페이지 즐겨찾기