C# FTP异步导致文件为零字节
我正在尝试使用AsyncWait上载一个文件(该文件作为字节数组传递)。我有一个工作正常的同步版本,但是异步版本在FTP服务器上创建了一个文件,但是没有字节 同步版本C# FTP异步导致文件为零字节,c#,asynchronous,async-await,ftp,C#,Asynchronous,Async Await,Ftp,我正在尝试使用AsyncWait上载一个文件(该文件作为字节数组传递)。我有一个工作正常的同步版本,但是异步版本在FTP服务器上创建了一个文件,但是没有字节 同步版本 public virtual void UploadFile(IFile file) { // Get the object used to communicate with the server. var request = (FtpWebRequest)WebRequest.Create("ftp://some
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);
}
}
异步的
正在同步调用导致问题的方法将以下内容添加到请求中:request.UseBinary=true@jdweng这是默认值-即使不是,如果同步代码工作,为什么它会与异步代码不同?@Rhodes73我已经用
byte[]
缓冲区而不是file.FileContents
测试了你的代码,它对我来说正常工作=>我们需要它顺便说一句,你为什么不干脆用or呢?
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();
}
}