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

查看:131
本文介绍了ASP.NET Web Api 2 - Internet Explorer:进程无法访问该文件,因为该文件正由另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Restful ASP.NET Web API 2,它使用以下方法返回带有图像内容的HttpResponseMessage:

I have a Restful ASP.NET Web API 2 with the following method that returns a HttpResponseMessage with an image as content:

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

该方法在Chrome / Opera /中正常运行Mozilla但在Internet Explorer(版本10/11)中,抛出以下错误:进程无法访问该文件,因为它正被另一个进程使用。

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".

我尝试了不同的关闭/处理流的方法,使用不同的文件访问属性并将内容设置为字节数组。但是,使用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制作。但是,我设法找到了解决方案。打开文件时,我错过了FileShare.Read属性。

It is weird that this behaviour only triggers when the request is made from Internet Explorer. However, I've managed to find a solution. I was missing the FileShare.Read attribute when opening the file.

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.

如果有人知道,请告诉我为什么这只发生在IE中。

If anyone know, please let me know why this only happens in IE.

这篇关于ASP.NET Web Api 2 - Internet Explorer:进程无法访问该文件,因为该文件正由另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆