开发者

How to pause and resume WebRequestMethods.Ftp.UploadFile process?

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 conten开发者_JAVA技巧tLen;
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.


You'd want to implement an asynchronous task.

First of all, read this article: http://aspalliance.com/1778

By using asynchronous tasks you'd be able to pause and/or resume a background thread, and let the thread processing the file upload request end, saving slots in IIS thread pool.

That pause and resume feature will be achieved by some synchronization logic.

For example, you can save somewhere the async task - formerly process - identifier, and prepare some boolean flag stored in a database, file or any storage available for you, and during each iteration in your upload loop, check that it has permission to continue.

If it doesn't have that permission, you can use a monitor, mutex or any other threading synchronization approach to wait a "pulse" for continuing the uploading process, or kill it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜