HTTP協(xié)議下用Web Service上傳大文件的解決方案
后來在一些網(wǎng)站上找到一些上傳文件的組件,但都是要用到一些COM組件。至于后來的ASP.net下上傳大文件的解決方案,我也做過一個(gè)組件,后來發(fā)現(xiàn)根本就不用自己寫什么組件,利用ASP.net自己的上傳方法也可以解決大文件上傳,真是郁悶的要死了。。。。
回想之后,決定用Web service來做一個(gè)文件上傳,還是利用HTTP協(xié)議,這樣不用在服務(wù)器上做太多的變動(dòng),而客戶端也簡(jiǎn)單。
首先是解決方案的設(shè)計(jì):因?yàn)閃eb service可以利用SOAP來傳遞數(shù)據(jù),而且可以傳遞十進(jìn)制數(shù)據(jù),因此可以想到,在服務(wù)上公開一個(gè)方法,參數(shù)可以是byte數(shù)組,這樣可以把文件分塊的上傳到服務(wù)器。這一解決方法我做過,但速度很慢。后來在MS上找到一些文章,用MS最新公開的服務(wù)組件上傳文件,速度快了很多。而自己所要做的就是組織一些安全性的問題。
部份代碼:Upload Instance
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();
}
}
}
}
上傳過程:
#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
測(cè)試項(xiàng)目代碼:
http://test.0579fw.com/myfile/kiyeer/客戶上傳/webbwinupload.zip
出現(xiàn)錯(cuò)誤的解決方法:
*****************************************************
引用內(nèi)容
Error 1 'WinFormTest.localhost.WebbWinUpload' does not contain a definition for 'RequestSoapContext' D:\WebbWinUpload\WinFormTest\WebbWinUpload.cs 448 19 WinFormTest
當(dāng)你更新Web引用的時(shí)候,.net自動(dòng)生成的Web引用為:
public class WebbWinUpload : System.Web.Services.Protocols.SoapHttpClientProtocol
請(qǐng)轉(zhuǎn)化為:
public class WebbWinUpload : Microsoft.Web.Services2.WebServicesClientProtocol
查找引用下自動(dòng)生成的C#文件Reference.cs
相關(guān)文章
.net中自定義錯(cuò)誤頁(yè)面的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于.net中自定義錯(cuò)誤頁(yè)面實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-06-061個(gè)文件如何輕松搞定Asp.net core 3.1動(dòng)態(tài)頁(yè)面轉(zhuǎn)靜態(tài)頁(yè)面
這篇文章主要給大家介紹了關(guān)于如何通過1個(gè)文件輕松搞定Asp.net core 3.1動(dòng)態(tài)頁(yè)面轉(zhuǎn)靜態(tài)頁(yè)面的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Asp.net core 3.1具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05ASP.NET Core依賴注入系列教程之控制反轉(zhuǎn)(IoC)
這篇文章主要給大家介紹了關(guān)于ASP.NET Core依賴注入系列教程之控制反轉(zhuǎn)(IoC)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11ASP.NET中基于soaphead的webservice安全機(jī)制
常會(huì)用到WebService來通訊,但WebService發(fā)布后為了能調(diào)用,一般都通過發(fā)布到IIS后調(diào)用 的。在IIS里可以通過匿名訪問,但這樣大家都可能訪問,不安全,下面提供一種基于soaphead的安全機(jī)制。2016-05-05ASP.NET實(shí)現(xiàn)根據(jù)URL生成網(wǎng)頁(yè)縮略圖的方法
這篇文章主要介紹了ASP.NET實(shí)現(xiàn)根據(jù)URL生成網(wǎng)頁(yè)縮略圖的方法,結(jié)合實(shí)例較為詳細(xì)的分析了asp.net生成網(wǎng)頁(yè)縮略圖的詳細(xì)實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2015-11-11Asp.Net?Core?使用Monaco?Editor?實(shí)現(xiàn)代碼編輯器功能
在項(xiàng)目中經(jīng)常有代碼在線編輯的需求,比如修改基于Xml的配置文件,編輯Json格式的測(cè)試數(shù)據(jù)等。這篇文章主要介紹了Asp.Net?Core?使用Monaco?Editor?實(shí)現(xiàn)代碼編輯器功能,需要的朋友可以參考下2022-01-01.net?程序通過?crontab?無法啟動(dòng)手動(dòng)執(zhí)行腳本啟動(dòng)的方法
.net 網(wǎng)關(guān)程序需要設(shè)置定時(shí)重啟,按照日常操作先把正在運(yùn)行的 PID kill 掉后,再執(zhí)行啟動(dòng)服務(wù)。通過腳本無法啟動(dòng),試著把 .net 程序?qū)懗煞?wù)后,發(fā)現(xiàn)是可以正常重啟的,本文給大家介紹下.net 程序通過 crontab 無法啟動(dòng)手動(dòng)執(zhí)行腳本啟動(dòng),感興趣的朋友一起看看吧2021-12-12XslTransform.Transform將結(jié)果輸出到字符串里的方法
XslTransform.Transform將結(jié)果輸出到字符串里的方法...2007-04-04