我正在尝试使用异步等待方式来上传文件(该文件以字节数组形式传递)。我有一个同步版本,可以正常工作,但异步版本在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();
}
}
这些方法被同步调用,这导致了问题