我的当前代码如下,仅下载文件。如何在浏览器中查看所有文件类型?
[HttpGet]
//[NoCacheHeader()]
[Route("api/image/files")]
public HttpResponseMessage GFiles(string ImageName)
{
var response = Request.CreateResponse(HttpStatusCode.OK);
var path = "~/Image/" + ImageName;
path = System.Web.Hosting.HostingEnvironment.MapPath(path);
var ext = System.IO.Path.GetExtension(path);
var contents = System.IO.File.ReadAllBytes(path);
System.IO.MemoryStream ms = new System.IO.MemoryStream(contents);
response.Content = new StreamContent(ms);
//
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = ImageName;
//
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("Image/" + ext);
return response;
}
答案 0 :(得分:1)
我找到了仅适用于pdf和图像的解决方案。只需在MediaTypeHeaderValue中添加MIME类型,并确保ContentDispositionHeaderValue为" inline"
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("inline");
for pdf:
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
图片:
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("images/jpeg");
根据图片类型改变您的MIME。 对于microsoft文档,在mediaheadervalue中使用它的mime类型,然后在google chrome中安装离线google docs扩展