文件句柄中的等待问题 Asp.Net
Problem with Await in File Handle Asp.Net
我创建了一个 Webform 应用程序,允许用户使用 AJAX 和 FileHandle.ashx 在 Asp.Net 中上传文件
我正在尝试使其 运行 异步,但效果不佳。
return“StartSavedEnd”下面的代码意味着等待不起作用,因为我的文件需要 10 秒才能上传。有什么建议吗?非常感谢
public override async Task ProcessRequestAsync(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Start");
await SaveFile(context);
context.Response.Write("End");
}
async Task SaveFile(HttpContext context)
{
var task = Task.Run(() =>
{
if (context.Request.Files.Count > 0)
{
HttpFileCollection files = context.Request.Files;
foreach (string key in files)
{
HttpPostedFile file = files[key];
string fileName = file.FileName;
fileName = context.Server.MapPath(fileName);
file.SaveAs(@"C:\TestZip.zip");
context.Response.Write("Saved");
}
}
});
await task;
}
I'm trying to make it run asynchronous but it doesn't work as well. The code below return "StartSavedEnd" which mean await doesn't work since my file take 10second to upload.
await
不会使文件上传 运行 更快,并且 await
doesn't return early from HTTP requests.
为了让您的系统看起来更灵敏,您需要在后台在客户端进行上传,例如,使用AJAX.
我创建了一个 Webform 应用程序,允许用户使用 AJAX 和 FileHandle.ashx 在 Asp.Net 中上传文件 我正在尝试使其 运行 异步,但效果不佳。 return“StartSavedEnd”下面的代码意味着等待不起作用,因为我的文件需要 10 秒才能上传。有什么建议吗?非常感谢
public override async Task ProcessRequestAsync(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Start");
await SaveFile(context);
context.Response.Write("End");
}
async Task SaveFile(HttpContext context)
{
var task = Task.Run(() =>
{
if (context.Request.Files.Count > 0)
{
HttpFileCollection files = context.Request.Files;
foreach (string key in files)
{
HttpPostedFile file = files[key];
string fileName = file.FileName;
fileName = context.Server.MapPath(fileName);
file.SaveAs(@"C:\TestZip.zip");
context.Response.Write("Saved");
}
}
});
await task;
}
I'm trying to make it run asynchronous but it doesn't work as well. The code below return "StartSavedEnd" which mean await doesn't work since my file take 10second to upload.
await
不会使文件上传 运行 更快,并且 await
doesn't return early from HTTP requests.
为了让您的系统看起来更灵敏,您需要在后台在客户端进行上传,例如,使用AJAX.