简体   繁体   English

ASP.NET Web Api 2 - Internet Explorer:该进程无法访问该文件,因为它正被另一个进程使用

[英]ASP.NET Web Api 2 - Internet Explorer: The process cannot access the file because it is being used by another process

I have a Restful ASP.NET Web API 2 with the following method that returns a HttpResponseMessage with an image as content:我有一个 Restful ASP.NET Web API 2,它使用以下方法返回一个带有图像作为内容的 HttpResponseMessage:

[AllowAnonymous]
[HttpGet, Route("{id}/image/"]
    public HttpResponseMessage GetImage(int id)
    {
        try
        {
            var artwork = Service.GetSingle(id);

            if(!isValidArtwork(artwork)) {
                 Request.CreateResponse(HttpStatusCode.NotFound); 
            }

            string mediaHeaderValue;
            switch (Path.GetExtension(artwork.filePath))
            {
                case ".jpg":
                    mediaHeaderValue = "image/jpg";
                    break;
                case ".jpeg":
                    mediaHeaderValue = "image/jpg";
                    break;
                case ".bmp":
                    mediaHeaderValue = "image/jpg";
                    break;
                case ".png":
                    mediaHeaderValue = "image/png";
                    break;
                default:
                    return new HttpResponseMessage(HttpStatusCode.NotFound);
            }

            using(var fileStream = File.Open(artwork.filePath, FileMode.Open, FileAccess.Read)){

                var response = new HttpResponseMessage { Content = new StreamContent(fileStream) };
                response.Content.Headers.ContentType = new MediaTypeHeaderValue(mediaHeaderValue);
                response.Content.Headers.ContentLength = fileStream.Length;

                return response;
            }
        }
        catch (Exception){
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }
    }

The method is working as intended in Chrome/Opera/Mozilla but in Internet Explorer (version 10/11), the following error is thrown: "The process cannot access the file because it is being used by another process".该方法在 Chrome/Opera/Mozilla 中按预期工作,但在 Internet Explorer(版本 10/11)中,抛出以下错误:“该进程无法访问该文件,因为它正被另一个进程使用”。

I've tried different ways of closing/disposing the stream, using different file access attributes and setting the content as a byte array.我尝试了不同的关闭/处理流的方法,使用不同的文件访问属性并将内容设置为字节数组。 However, the same error appears when using Internet Explorer (version 10/11).但是,在使用 Internet Explorer(版本 10/11)时会出现相同的错误。

Any suggestions on how this can be resolved?有关如何解决此问题的任何建议? Or had similar problems with Internet Explorer (version 10/11)?或者 Internet Explorer(版本 10/11)有类似问题?

It is weird that this behaviour only triggers when the request is made from Internet Explorer.奇怪的是,此行为仅在从 Internet Explorer 发出请求时触发。 However, I've managed to find a solution.但是,我设法找到了解决方案。 I was missing the FileShare.Read attribute when opening the file.打开文件时我缺少 FileShare.Read 属性。

var fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

I've found another solution too.我也找到了另一个解决方案。 Copying the image data to a byte array, and disposing the file stream.将图像数据复制到字节数组,并处理文件流。 Then use the byte array as content of the response message.然后使用字节数组作为响应消息的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp.Net文件流IO异常:由于另一个进程正在使用该进程,因此无法访问文件 - Asp.Net File Stream IO Exception : Cannot access the file because the process is being used by another process 该进程无法访问该文件,因为它正被另一个进程asp.net c#使用? - The process cannot access the file because it is being used by another process asp.net c#? ASP.net该进程无法访问该文件,因为该文件正在被另一个进程使用 - ASP.net The process cannot access the file because it is being used by another process 无法删除文件,因为它正在被另一个进程ASP.NET Core MVC使用 - Cannot delete file because it is being used by another process, ASP.NET Core MVC 该进程无法访问文件.exe,因为它正在被另一个进程使用 - The process cannot access the file .exe because it is being used by another process 该进程无法访问&#39;xml&#39;文件,因为它正由另一个进程使用 - The process cannot access the 'xml' file because it is being used by another process 进程无法访问文件 <filepath> 因为它正在被另一个进程使用 - The process cannot access the file <filepath> because it is being used by another process 该进程无法访问该文件,因为该文件正在被另一个进程使用 - The process cannot access the file because it is being used by another process2 该进程无法访问该文件,因为该文件正由另一个进程使用 - The process cannot access the file because it is being used by another process 该进程无法访问该文件,因为它正在被另一个进程使用 - The process cannot access the file because it is being used by another process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM