C# 异步/等待加载文件
我正在尝试加载具有异步/等待模式的zipfile:C# 异步/等待加载文件,c#,wpf,C#,Wpf,我正在尝试加载具有异步/等待模式的zipfile: private async void button2_Click(object sender, RoutedEventArgs e) { await processFtp(); } async Task processFtp() { string result = ""; string ftpHost = "ftp://mysite/mysubdir";
private async void button2_Click(object sender, RoutedEventArgs e)
{
await processFtp();
}
async Task processFtp()
{
string result = "";
string ftpHost = "ftp://mysite/mysubdir";
string ftpUser = "itsme";
string ftpPassword = "mypw";
string ftpfullpath = ftpHost + "/" + "OutdoorTest.zip";
string fileToUpload = @"c:\temp\Outdoorbilder.zip";
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpfullpath);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(ftpUser,ftpPassword);
request.UseBinary = true;
request.KeepAlive = false;
request.ReadWriteTimeout = 1000000;
request.Timeout = 1000000;
using (Stream requestStream = request.GetRequestStream())
{
using (FileStream fs = File.OpenRead(fileToUpload))
{
byte[] b = new byte[10 * 1024];
int readLength = 0;
int sentLength = 0;
while ((readLength = fs.Read(b, 0, b.Length)) > 0)
{
await requestStream.WriteAsync(b, 0, b.Length);
int percentComplete = (int)((float)(sentLength += readLength) / (float)fs.Length * 100);
ftpprogress.Value = percentComplete;
}
requestStream.Close();
requestStream.Flush();
}
}
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
result = response.StatusDescription;
}
catch (WebException e)
{
result = e.Message;
if (e.Status == WebExceptionStatus.ProtocolError)
{
result = result + "Status Code : " +
((FtpWebResponse)e.Response).StatusCode;
result = result + "\nStatus Description : " +
((FtpWebResponse)e.Response).StatusDescription;
}
}
catch (Exception e)
{
result = e.Message;
}
MessageBox.Show(result);
}
}
代码似乎工作正常,我得到了226的回复。但ftp服务器上的zip文件比原始文件大1000字节左右,下载到移动android设备后无法打开/提取
当我在没有异步/等待模式的情况下上传时,上传的文件在ftp服务器上的大小与本地原始文件的大小相同
这是如何发生的/在哪里发生的?这与异步/等待无关 你的问题是你没有告诉正确的大小上传。看看这两行:
while ((readLength = fs.Read(b, 0, b.Length)) > 0)
{
await requestStream.WriteAsync(b, 0, b.Length);
您需要指定WriteAsyc
写入读取量,而不是为字节缓冲区分配的量。至少最后一次读取返回的值小于缓冲区大小
因此正确的代码是:
while ((bytesRead = fs.Read(b, 0, b.Length)) > 0)
{
await requestStream.WriteAsync(b, 0, bytesRead);