好的,我有这段代码可以处理通过简单的网络表单上传的文件
CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(@"C:\inetpub\wwwroot\myapp\Images");
//var provider = new MultipartFormDataStreamProvider(@"C:\inetpub\wwwroot\myapp\Images");
var Image = "";
var Dir = "";
var CurrentPath = "";
string UploadType = "";
string ImageName = "";
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider);
// Show all the key-value pairs.
foreach (var key in provider.FormData.AllKeys)
{
foreach (var val in provider.FormData.GetValues(key))
{
if (key == "uploadType") UploadType = val;
if (key == "imageName") ImageName = val;
//Trace.WriteLine(string.Format("{0}: {1}", key, val));
}
}
foreach (MultipartFileData file in provider.FileData)
{
//Debug.WriteLine(file.Headers.ContentDisposition.FileName);
//Debug.WriteLine("Server file path: " + file.LocalFileName);
Image = Path.GetFileName(file.LocalFileName);
Dir = Path.GetDirectoryName(file.LocalFileName);
CurrentPath = file.LocalFileName;
}
if (UploadType == "update")
{
File.Delete(Dir + "\\" + Image);
File.Move(CurrentPath, Dir + "\\" + Image);
Imagen = ImageName;
}
return Request.CreateResponse(HttpStatusCode.OK, Image);
正如一些文档提到的那样,使用 await 会导致读取过程异步,我的问题是我需要知道文件何时保存到文件夹中,因为如果要更新UploadType
表单字段,我想删除它==
,但似乎就像尝试这条线一样
File.Delete(Dir + "\\" + Image);
图像尚未保存,我收到图像不存在的错误,所以我的问题是,我如何判断或确保图像在目录中准备好?我怎么知道异步进程结束了,谢谢你的帮助!!