HTTP协议下用Web Service上传大文件的解决方案

用HTTP协议上传大文件也许是个不好办的问题。主要是它的不连续性,使得上传文件感觉很“危险”。特别是很大的文件(几百MB甚至是上G的文件),心里总觉得不踏实,一不小心就会出现问题,而一但出现问题就无法继续上传,这是很郁闷的。

后来在一些网站上找到一些上传文件的组件,但都是要用到一些COM组件。至于后来的ASP.net下上传大文件的解决方案,我也做过一个组件,后来发现根本就不用自己写什么组件,利用ASP.net自己的上传方法也可以解决大文件上传,真是郁闷的要死了。。。。

回想之后,决定用Web service来做一个文件上传,还是利用HTTP协议,这样不用在服务器上做太多的变动,而客户端也简单。

首先是解决方案的设计:因为Web service可以利用SOAP来传递数据,而且可以传递十进制数据,因此可以想到,在服务上公开一个方法,参数可以是byte数组,这样可以把文件分块的上传到服务器。这一解决方法我做过,但速度很慢。后来在MS上找到一些文章,用MS最新公开的服务组件上传文件,速度快了很多。而自己所要做的就是组织一些安全性的问题。

部份代码:Upload Instance

None.gif using  System;
None.gif
using  System.IO;
None.gif
using  Microsoft.Web.Services2;
None.gif
using  Microsoft.Web.Services2.Dime;
None.gif
None.gif
namespace  Webb.WAVE.WinUpload
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for Controls.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class UploadInstance2
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Fields#region Fields
InBlock.gif        
private string m_GUID;
InBlock.gif        
private DateTime m_uploadTime;
InBlock.gif        
private long m_fileLength;
InBlock.gif        
private long m_currentPoint;
InBlock.gif        
private string m_pathOnserver;
InBlock.gif        
private long m_userID;
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Properties#region Properties
InBlock.gif        
public long UserID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return this.m_userID;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{this.m_userID=value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string GUID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return this.m_GUID;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{this.m_GUID=value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public DateTime UploadTime
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return this.m_uploadTime;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public long FileLength
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return this.m_fileLength;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{this.m_fileLength=value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public long CurrentPoing
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return this.m_currentPoint;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{this.m_currentPoint=value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string PathOnServer
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return this.m_pathOnserver;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{this.m_pathOnserver=value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string FullPathOnServer
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(this.m_GUID!=string.Empty&&this.m_pathOnserver!=string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return Path.Combine(this.m_pathOnserver,this.m_GUID+".rem");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return string.Empty;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string FileName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(this.m_GUID!=string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return this.m_GUID+".rem";
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return string.Empty;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
public UploadInstance2()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.m_GUID            = System.Guid.NewGuid().ToString();
InBlock.gif            
this.m_uploadTime    = System.DateTime.Now;
InBlock.gif            
this.m_currentPoint    = 0;
InBlock.gif            
this.m_fileLength    = 0;
InBlock.gif            
this.m_pathOnserver    = string.Empty;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public UploadInstance2(string i_path,string i_GUID,long i_fileLength)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string m_fullPath    = Path.Combine(i_path,i_GUID);
InBlock.gif            
if(!File.Exists(m_fullPath)) return;
InBlock.gif            
this.m_GUID            = i_GUID;
InBlock.gif            
this.m_uploadTime    = System.DateTime.Now;
InBlock.gif            
this.m_pathOnserver    = i_path;
InBlock.gif            FileInfo m_fileInfo    
= new FileInfo(m_fullPath);
InBlock.gif            
this.m_currentPoint    = m_fileInfo.Length;
InBlock.gif            
this.m_fileLength    = i_fileLength;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool UploadData(byte[] i_data, long i_currentPoint, int i_dataSize)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string m_fullPath        = this.FullPathOnServer;
InBlock.gif            
if(!File.Exists(m_fullPath)&&this.m_currentPoint!=0)return false;
InBlock.gif            
long m_filePoint        = new FileInfo(m_fullPath).Length;
InBlock.gif            
if(m_filePoint!=i_currentPoint) return false;
InBlock.gif            FileStream m_fileStream    
= new FileStream(m_fullPath,FileMode.Append);
InBlock.gif            m_fileStream.Write(i_data,
0,i_dataSize);
InBlock.gif            m_fileStream.Close();
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void AbandantUpload()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string m_fullPath        = this.FullPathOnServer;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
trydot.gif{File.Delete(m_fullPath);}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
catchdot.gif{}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void CreateFile()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string m_fullPath        = this.FullPathOnServer;
InBlock.gif            
if(!File.Exists(m_fullPath))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                File.Create(m_fullPath).Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    File.Delete(m_fullPath);
ExpandedSubBlockStart.gifContractedSubBlock.gif                }
catchdot.gif{}
InBlock.gif                File.Create(m_fullPath).Close();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

上传过程:

ContractedBlock.gif ExpandedBlockStart.gif UploadProcess #region UploadProcess
InBlock.gif        
public void UploadProcess()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DateTime m_start    
= DateTime.Now;
InBlock.gif            
this.textBox_OutMsg.AppendText("Initialize uploaddot.gif\r\n");
InBlock.gif            
if(this.m_upload==null||this.m_uploadGUID==null||this.m_uploadGUID==string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{                
InBlock.gif                
this.textBox_OutMsg.AppendText("Upload instance id error or login to the server failddot.gif\r\n");
InBlock.gif                
this.textBox_OutMsg.AppendText("Upload faild.\r\n");
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
this.textBox_OutMsg.AppendText("Open filedot.gif\r\n");
InBlock.gif            
if(this.m_filePath==null||this.m_filePath==string.Empty||!File.Exists(this.m_filePath))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.textBox_OutMsg.AppendText("Open file errordot.gif\r\n");
InBlock.gif                
this.textBox_OutMsg.AppendText("Upload faild.\r\n");
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            FileInfo m_fileInfo    
= new FileInfo(this.m_filePath);
InBlock.gif            FileStream m_fs        
= new FileStream(this.m_filePath, FileMode.Open, FileAccess.Read);
InBlock.gif            
this.textBox_OutMsg.AppendText("Start upload filedot.gif\r\n");
InBlock.gif            
int m_buffer        = 10;    //KBytes
InBlock.gif
            long m_currentPoint    = 0;
InBlock.gif            
long m_fileLength    = m_fileInfo.Length;
InBlock.gif            
bool m_uploadResult    = false;
InBlock.gif            
byte[] m_data        = new byte[m_buffer*1024];
InBlock.gif            
long m_readBytes    = m_fs.Read(m_data, 0, m_buffer*1024);
InBlock.gif            
this.UploadProcessBar.Maximum    = 100;
InBlock.gif            
this.UploadProcessBar.Minimum    = 0;
InBlock.gif            
while(m_readBytes>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MemoryStream m_memoryStream 
= new MemoryStream(m_data, 0,(int)m_readBytes);
InBlock.gif                DimeAttachment dimeAttach    
= new DimeAttachment("image/gif", TypeFormat.MediaType, m_memoryStream);
InBlock.gif                
this.m_upload.RequestSoapContext.Attachments.Add(dimeAttach);
InBlock.gif                m_uploadResult                
= this.m_upload.UploadFileData(this.m_uploadInstance,m_currentPoint,m_readBytes);
InBlock.gif                
if(m_uploadResult)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    m_currentPoint    
+=m_readBytes;
InBlock.gif                    m_readBytes        
= m_fs.Read(m_data,0,m_buffer*1024);
InBlock.gif                
//    this.textBox_OutMsg.AppendText("Uploading:"+m_currentPoint.ToString()+"/"+m_fileLength.ToString()+"\r\n");
InBlock.gif
                    this.UploadProcessBar.Value = (int)(m_currentPoint*100/m_fileLength);
InBlock.gif                    
this.label_outPercent.Text    = this.UploadProcessBar.Value.ToString()+"%";
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.textBox_OutMsg.AppendText("Upload file errordot.gif.\r\n");
InBlock.gif                    m_fs.Close();
InBlock.gif                    
this.m_upload.AbandantUpload(this.m_uploadInstance);
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

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

ExpandedBlockEnd.gif        
#endregion

WebbWinUpload.JPG

测试项目代码:
http://files.cnblogs.com/WuCountry/WebbWinUpload.zip

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值