好的我有这个代码来处理通过简单的Web表单上传的文件
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);
图像尚未保存,我收到图像不存在的错误,所以我的问题是,我如何判断或确定图像在dir中已准备就绪?我怎么能告诉异步过程结束,谢谢你对此有任何帮助!!
答案 0 :(得分:1)
File.Exists
是您应该使用的方法。
除了您应该使用Path.Combine
而不是使用连接构建路径。