我开发了ASP.NET web API。我正在尝试读取excel文件的内容并尝试将其作为字节返回。我收到以下错误:该进程无法访问文件XXX,因为它正被另一个进程在asp.net web api中使用
The process cannot access the file 'C:\app\MyHost.AppServices\bin\Debug\temp\888.xlsx' because it is being used by another process.
我正在使用下面的code.I不知道是什么导致此错误。请提供您的建议
public class FileController : MyBase
{
public HttpResponseMessage Get(string id)
{
if (String.IsNullOrEmpty(id))
return Request.CreateResponse(HttpStatusCode.BadRequest);
var path = Path.Combine("temp", id);
var fileStream = File.Open(path, FileMode.Open);
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
//response.Content = new StreamContent(fileStream);
response.Content = new StreamContent(new FileStream(path, FileMode.Open, FileAccess.ReadWrite));
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = id;
return response;
}
}
您的fileStream变量未使用,创建StreamContent实例时还创建了新的FileStream实例,该实例未处理。 – Giedrius