0

How to pause and resume FTP upload process? My Upload process is the following code. How to implement pause and resume the process?

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();

Thank you.

4

1 回答 1

1

你想实现一个异步任务。

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

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

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

例如,您可以将异步任务(以前的进程)标识符保存在某处,并准备一些布尔标志存储在数据库、文件或任何可用的存储中,并在上传循环的每次迭代期间,检查它是否有权继续.

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

于 2011-01-31T08:45:28.090 回答