2011-01-31 55 views
0

如何暂停和恢复FTP上传过程? 我的上传过程如下代码。如何实现暂停并恢复过程?如何暂停和恢复WebRequestMethods.Ftp.UploadFile过程?

FileInfo fileInf = new FileInfo(filename); 
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + host + defaultDir + "/" + fileInf.Name)); 

reqFTP.Credentials = new NetworkCredential(user, pass); 
reqFTP.KeepAlive = false; 
reqFTP.Method = WebRequestMethods.Ftp.UploadFile; 
reqFTP.UseBinary = true; 
reqFTP.ContentLength = fileInf.Length; 

int buffLength = 2048; 
byte[] buff = new byte[buffLength]; 
int contentLen; 
FileStream fs = fileInf.OpenRead(); 
Stream strm = reqFTP.GetRequestStream(); 
contentLen = fs.Read(buff, 0, buffLength); 
int maxLen = contentLen; 
while (contentLen != 0) 
{ 
       // Write Content from the file stream to the FTP Upload Stream 
       strm.Write(buff, 0, contentLen); 
       contentLen = fs.Read(buff, 0, buffLength); 
} 

strm.Close(); 
fs.Close(); 

谢谢。

回答

1

你想实现一个异步任务。

首先,阅读这篇文章: http://aspalliance.com/1778

通过使用异步任务你可以暂停和/或恢复 后台线程,并让该线程处理文件上传 请求结束,节省IIS线程池中的插槽。

暂停和恢复功能将通过一些同步逻辑来实现。

例如,可以保存在某个地方的异步任务 - 原工艺 - 标识符, 并准备存储在数据库中,文件或任何存储 为您提供一些布尔标志,并在上传循环,检查每次迭代中 它有权继续。

如果它没有该权限,可以使用监视器,互斥锁或任何其他线程同步方法来等待“脉冲”,以继续上传过程或终止上传过程。