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
我有一个 Restful ASP.NET Web API 2,使用以下方法 returns 一个以图像作为内容的 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);
}
}
该方法在 Chrome/Opera/Mozilla 中按预期工作,但在 Internet Explorer(版本 10/11)中,抛出以下错误:"The process cannot access the file because it is being used by another process".
我尝试了 closing/disposing 流的不同方式,使用不同的文件访问属性并将内容设置为字节数组。但是,使用 Internet Explorer(版本 10/11)时出现相同的错误。
关于如何解决这个问题有什么建议吗?或者 Internet Explorer(版本 10/11)有类似的问题?
奇怪的是,只有在从 Internet Explorer 发出请求时才会触发此行为。但是,我设法找到了解决方案。打开文件时我缺少 FileShare.Read 属性。
var fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
我也找到了另一个解决方案。将图像数据复制到字节数组,并处理文件流。然后使用字节数组作为响应消息的内容。
我有一个 Restful ASP.NET Web API 2,使用以下方法 returns 一个以图像作为内容的 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);
}
}
该方法在 Chrome/Opera/Mozilla 中按预期工作,但在 Internet Explorer(版本 10/11)中,抛出以下错误:"The process cannot access the file because it is being used by another process".
我尝试了 closing/disposing 流的不同方式,使用不同的文件访问属性并将内容设置为字节数组。但是,使用 Internet Explorer(版本 10/11)时出现相同的错误。
关于如何解决这个问题有什么建议吗?或者 Internet Explorer(版本 10/11)有类似的问题?
奇怪的是,只有在从 Internet Explorer 发出请求时才会触发此行为。但是,我设法找到了解决方案。打开文件时我缺少 FileShare.Read 属性。
var fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
我也找到了另一个解决方案。将图像数据复制到字节数组,并处理文件流。然后使用字节数组作为响应消息的内容。