FTP异步导致文件为零字节

问题描述 投票:1回答:1

我正在尝试使用异步等待方式来上传文件(该文件以字节数组形式传递)。我有一个同步版本,可以正常工作,但异步版本在FTP服务器上创建了一个文件,但为零字节。

同步版本

public virtual void UploadFile(IFile file)
{
    // Get the object used to communicate with the server.
    var request = (FtpWebRequest)WebRequest.Create("ftp://someFtpsite.com");
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.Credentials = Credentials;
    request.ContentLength = file.FileContents.Length;

    using (Stream requestStream = request.GetRequestStream())
    {
        requestStream.Write(file.FileContents, 0, file.FileContents.Length);
    }
}

异步

public virtual async Task UploadFileAsync(IKCFile file)
{
    // Get the object used to communicate with the server.
    var request = (FtpWebRequest)WebRequest.Create("ftp://someFtpsite.com");
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.Credentials = Credentials;
    request.ContentLength = file.FileContents.Length;

    using (Stream requestStream = await request.GetRequestStreamAsync())
    {
        await requestStream.WriteAsync(file.FileContents, 0, file.FileContents.Length);
        await requestStream.FlushAsync();
    }
}
c# asynchronous async-await ftp
1个回答
0
投票

这些方法被同步调用,这导致了问题

© www.soinside.com 2019 - 2024. All rights reserved.