我已尝试使用Response.Redirect为用户下载.dat文件的代码。 但是,它总是让我看到这个错误。下载.dat文件的代码
The page you are requesting cannot be served because of the extension
configuration. If the page is a script, add a handler. If the file should be
downloaded, add a MIME map.
对于Response.Redirect的
Response.Redirect(filepath, false);
我的示例代码,我不没有什么不妥的地方。另外,我可以使用此代码下载.xlsx文件。
感谢您的帮助。
这是更新的代码,没有响应下载。
编辑的代码
string fpath = Server.MapPath("data") + "\\" + filename + ".dat";
if (File.Exists(fpath))
{
File.Delete(fpath);
}
StreamWriter swExtLogFile = new StreamWriter(fpath, true);
try
{
int i;
foreach (DataRow row in tbl.Rows)
{
object[] array = row.ItemArray;
for (i = 0; i < array.Length - 1; i++)
{
swExtLogFile.Write(array[i].ToString());
}
swExtLogFile.WriteLine(array[i].ToString());
}
swExtLogFile.Close();
swExtLogFile.Dispose();
FileInfo fInfo = new FileInfo(fpath);
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Length", fInfo.Length.ToString());
Response.AppendHeader("content-disposition",
string.Format("attachment;filename={0}", fInfo.Name));
Response.Flush();
Response.TransmitFile(fInfo.FullName);
HttpContext.Current.ApplicationInstance.CompleteRequest();
你在IIS上检查过添加MIME类型吗? – Jacky
[“您正在请求的页面由于扩展配置而无法投放”。错误消息](https://stackoverflow.com/questions/4388066/the-page-you-are-requesting-cannot-be-served-because-of-the-extension-configura) – BACON