[英]Force open a text file from javascript (file is on server)
单击生成按钮后,我正在生成多个文件。 成功生成文件后,将其存储在服务器上,将为用户提供相同的文件,以便他可以下载该文件。
我尝试将扩展名更改为无法通过浏览器直接打开的已知扩展名,即.doc。 这作为显示要下载文件的弹出窗口而起作用。
但是,我想将扩展名更改为更有意义的扩展名,例如.cfg。 问题在于,使用.cfg或.txt时,浏览器会自动打开文件(因为它是文本文件)。 如何覆盖呢?
以下是代码:
C#服务器端:
string[] L_TextFile = P_Data.M_Content.Split('|');
System.IO.File.WriteAllLines(@"c:\files\file.cfg", L_TextFile);
response = "1";
return response;
extjs ajax调用成功代码:
success: function(P_Response){
var res = Ext.util.JSON.decode(P_Response.responseText);
if(res == '1') window.location.href'/files/file.cfg';
else ShowError('An error has occured while generating the text file.');
},
基本上,.doc一切正常,但我想将其更改为.cfg
有什么帮助吗?
将响应MIME类型设置为"application/octet-stream"
并可以选择设置文件名。
例:
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
response.AddHeader("Content-Type", "application/octet-stream");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.