0

好的,我有这段代码可以处理通过简单的网络表单上传的文件

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);

图像尚未保存,我收到图像不存在的错误,所以我的问题是,我如何判断或确保图像在目录中准备好?我怎么知道异步进程结束了,谢谢你的帮助!!

4

1 回答 1

1

File.Exists是您应该使用的方法。

除了您应该使用Path.Combine连接而不是构建路径。

于 2013-02-27T15:24:40.437 回答