我需要浏览器打开它直接在浏览器中理解的文件类型(即没有“打开/保存/取消”对话框。
这是我的代码,目前效果很好!...除了每个文件都会弹出对话框并且不直接打开文件:
string filePath = Path.Combine(WebConfigurationManager.AppSettings["NewsAttachmentPath"], context.Request.QueryString["FileName"]);
byte[] bytes = System.IO.File.ReadAllBytes(filePath);
context.Response.Clear();
context.Response.ContentType = "application/octet-stream";
context.Response.Cache.SetCacheability(HttpCacheability.Private);
context.Response.Expires = -1;
context.Response.Buffer = true;
context.Response.AddHeader("Content-Disposition", string.Format("{0};FileName=\"{1}\"", "inline", context.Request.QueryString["FileName"]));
context.Response.BinaryWrite(bytes);
context.Response.End();
如您所见,即使我将 Content-Disposition 更改为“内联”,它仍然会提示下载。这是我知道我的浏览器可以理解的文件。换句话说,我可以去某个随机站点并单击 PDF,它会在浏览器中打开。我的网站会让我保存它以便查看它。
先发制人地回答“为什么要使用 application/octet-stream?” 因为我不想为每个文件类型创建一个处理程序。如果这是错误的,请告诉我。