我有一些从 SO 帖子和其他网站收集的 Web API 代码。但是,任务的东西对我来说仍然是新的。我正在尝试将上传的文件复制到新位置,但有时(并非总是)我在尝试复制文件时遇到异常。异常表明该文件正在被另一个进程使用。不过,它并非每次都会发生。我想我需要将复制操作移到其他地方。这是我的代码。有什么建议么?
var provider = new MultipartFormDataStreamProvider(uploadroot);
var task = Request.Content.ReadAsMultipartAsync(provider).ContinueWith<HttpResponseMessage>(t =>
{
if (t.IsFaulted || t.IsCanceled)
throw new HttpResponseException(HttpStatusCode.InternalServerError);
var docConversionId = Guid.NewGuid().ToString("N");
var sourceFilePath = Path.Combine(uploadroot, provider.FileData.First().LocalFileName);
var destinationFilePath = Path.Combine(inboxroot, docConversionId);
File.Copy(sourceFilePath, destinationFilePath);
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StringContent(docConversionId);
//response.Content.Headers.Add("DocumentConversionId", docConversionId);
return response;
});
return task;