无为清净楼资源网 Design By www.qnjia.com
用HTTP协议上传大文件也许是个不好办的问题。主要是它的不连续性,使得上传文件感觉很“危险”。特别是很大的文件(几百MB甚至是上G的文件),心里总觉得不踏实,一不小心就会出现问题,而一但出现问题就无法继续上传,这是很郁闷的。
后来在一些网站上找到一些上传文件的组件,但都是要用到一些COM组件。至于后来的ASP.net下上传大文件的解决方案,我也做过一个组件,后来发现根本就不用自己写什么组件,利用ASP.net自己的上传方法也可以解决大文件上传,真是郁闷的要死了。。。。
回想之后,决定用Web service来做一个文件上传,还是利用HTTP协议,这样不用在服务器上做太多的变动,而客户端也简单。
首先是解决方案的设计:因为Web service可以利用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>
/// 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
测试项目代码:
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
当你更新Web引用的时候,.net自动生成的Web引用为:
public class WebbWinUpload : System.Web.Services.Protocols.SoapHttpClientProtocol
请转化为:
public class WebbWinUpload : Microsoft.Web.Services2.WebServicesClientProtocol
查找引用下自动生成的C#文件Reference.cs
后来在一些网站上找到一些上传文件的组件,但都是要用到一些COM组件。至于后来的ASP.net下上传大文件的解决方案,我也做过一个组件,后来发现根本就不用自己写什么组件,利用ASP.net自己的上传方法也可以解决大文件上传,真是郁闷的要死了。。。。
回想之后,决定用Web service来做一个文件上传,还是利用HTTP协议,这样不用在服务器上做太多的变动,而客户端也简单。
首先是解决方案的设计:因为Web service可以利用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>
/// 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
测试项目代码:
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
当你更新Web引用的时候,.net自动生成的Web引用为:
public class WebbWinUpload : System.Web.Services.Protocols.SoapHttpClientProtocol
请转化为:
public class WebbWinUpload : Microsoft.Web.Services2.WebServicesClientProtocol
查找引用下自动生成的C#文件Reference.cs
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
2024年11月17日
2024年11月17日
- 中国武警男声合唱团《辉煌之声1天路》[DTS-WAV分轨]
- 紫薇《旧曲新韵》[320K/MP3][175.29MB]
- 紫薇《旧曲新韵》[FLAC/分轨][550.18MB]
- 周深《反深代词》[先听版][320K/MP3][72.71MB]
- 李佳薇.2024-会发光的【黑籁音乐】【FLAC分轨】
- 后弦.2012-很有爱【天浩盛世】【WAV+CUE】
- 林俊吉.2012-将你惜命命【美华】【WAV+CUE】
- 晓雅《分享》DTS-WAV
- 黑鸭子2008-飞歌[首版][WAV+CUE]
- 黄乙玲1989-水泼落地难收回[日本天龙版][WAV+CUE]
- 周深《反深代词》[先听版][FLAC/分轨][310.97MB]
- 姜育恒1984《什么时候·串起又散落》台湾复刻版[WAV+CUE][1G]
- 那英《如今》引进版[WAV+CUE][1G]
- 蔡幸娟.1991-真的让我爱你吗【飞碟】【WAV+CUE】
- 群星.2024-好团圆电视剧原声带【TME】【FLAC分轨】