该进程无法访问该文件,因为Web.api文件流中的另一个进程正在使用该文件

时间:2019-03-19 17:41:21

标签: asp.net-web-api asp.net-web-api2 multipartform-data ioexception form-data

我有这段代码:

[AcceptVerbs("POST")]
[Route("api/doc/ImageUpload")]
public HttpResponseMessage ImageUpload()
{
    try
    {
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        string root = HttpContext.Current.Server.MapPath("~/imageUpload");
        var provider = new MultipartFormDataStreamProvider(root);

        Request.Content.ReadAsMultipartAsync(provider);

            foreach (MultipartFileData file in provider.FileData)
            {

                if (string.IsNullOrEmpty(file.Headers.ContentDisposition.FileName))
                {
                    return Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted");
                }

                string fileName = file.Headers.ContentDisposition.FileName;
                if (fileName.StartsWith("\"") && fileName.EndsWith("\""))
                {
                    fileName = fileName.Trim('"');
                }
                if (fileName.Contains(@"/") || fileName.Contains(@"\"))
                {
                    fileName = Path.GetFileName(fileName);
                }

                using (FileStream stream = File.Open(file.LocalFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    //do something
                }
            }
    }  
}

这个邮递员电话:

enter image description here

我可以看到该文件夹​​中正在生成文件:

enter image description here

但是当试图在文件流中实际使用文件并将其另存为正确的jpeg或png时,我得到了IOException。另外我还注意到保存的文件是32kb,但是我要上传的文件是120kb。

0 个答案:

没有答案