0

单击生成按钮后,我正在生成几个文件。在成功生成该文件然后存储在服务器上之后,用户会看到相同的文件,以便他可以下载它。

我尝试将扩展名更改为浏览器无法直接打开的已知扩展名,即.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

有什么帮助吗?

4

1 回答 1

3

将响应 MIME 类型设置为"application/octet-stream"并可选择设置文件名。

例子:

response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
response.AddHeader("Content-Type", "application/octet-stream");
于 2012-07-12T14:03:51.510 回答