【发布时间】:2013-07-02 04:19:49
【问题描述】:
我有一个技术问题要问你们中的一些人。
基本上我主要在 Firefox 中测试我的应用程序,我在应用程序中有一个下载功能,用户可以从 SQL Server 数据库下载文件。问题是当我在 Internet Explorer 中下载文件时,除了 Microsoft 文件之外,每个文件都会丢失其扩展名,例如word、access等。firefox除了位图没有问题...
这是我的代码,谢谢
//Download attachment file
protected void AttachmentDLBut_Click(object sender, EventArgs e)
{
//set the ID for the selected row
var ID = Request.QueryString["Id"];
using (SqlConnection conn = new SqlConnection("******"))
{
string sql = "SELECT FileType, Filename, Description FROM NetworkEquipment WHERE NetworkEquipmentID = '" + ID + "'";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
//cmd.Parameters.AddWithValue("@ID", AttachmentDDL.SelectedItem.Value);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
try
{
//if there is an attachment... download it
if (dr.Read())
{
Response.Clear();
Response.ContentType = dr["FileType"].ToString();
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["Filename"].ToString());
Response.BinaryWrite((byte[])dr["Description"]);
Response.End();
conn.Close();
}
}
catch (SqlException sqlex) { MessageBox.Show("Sorry, an unexpected error has occurred while downloading this file. Error: " + sqlex); }
catch (Exception ex) { MessageBox.Show("Sorry, an unexpected error has occurred while downloading this file. Error: " + ex); }
//else nothing happens
}
}
}
【问题讨论】:
-
这个帖子似乎和你有同样的问题stackoverflow.com/questions/5198530/…
-
您应该尝试设置
ContentType,有时浏览器会忽略扩展名,而直接使用ContentType。 -
filename=后面的\"应该去掉,或者在dr["Filename"].ToString()之后关闭
-
问题已解决,感谢您的意见
标签: c# asp.net sql sql-server