asp.net c#应用程序中浏览器之间的文件扩展名丢失

asp.net c#应用程序中浏览器之间的文件扩展名丢失,c#,asp.net,sql,sql-server,C#,Asp.net,Sql,Sql Server,我有一个技术问题要问你们中的一些人 基本上,我主要在firefox中测试我的应用程序,我在应用程序中有一个下载功能,用户可以从SQL server数据库下载文件。问题是,当我在Internet explorer中下载文件时,除了Microsoft文件(例如word、access等)外,每个文件都将丢失其扩展名。除了位图,firefox中没有任何问题 这是我的密码,谢谢 //Download attachment file protected void AttachmentDLBu

我有一个技术问题要问你们中的一些人

基本上,我主要在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
            }
        }
    }

使用F12开发工具或FireBug或类似工具查看服务器实际发送的内容。在这种情况下,它将类似于:

Content-Disposition: attachment;filename="filename.ext
默认情况下,服务器也可以发送此信息或类似信息:

Content-Type: text/html
首先,您需要关闭文件名后的引号。建议在分号后加空格。您还需要清理文件名,以确保它不包含混淆或非法字符。至少你需要去掉双引号或者引用它们

其次,您需要添加一个内容类型头。骗局是设置

Content-Type: application/octet-stream 

这将导致浏览器根据文件扩展名猜测。

使用F12开发工具或FireBug或类似工具查看服务器实际发送的内容。在这种情况下,它将类似于:

Content-Disposition: attachment;filename="filename.ext
默认情况下,服务器也可以发送此信息或类似信息:

Content-Type: text/html
首先,您需要关闭文件名后的引号。建议在分号后加空格。您还需要清理文件名,以确保它不包含混淆或非法字符。至少你需要去掉双引号或者引用它们

其次,您需要添加一个内容类型头。骗局是设置

Content-Type: application/octet-stream 

这将导致浏览器根据文件扩展名猜测。请再次检查文件名。如果文件下载有1个字符是空格键(“”),则示例:“template import.xls”。Firefox在下载时会出现没有扩展的错误。您可以使用函数替换(“,”)来解决此问题:


Response.AddHeader(“内容处置”、“附件;文件名=\”“+replace(dr[“filename”].ToString(),”“,”“);

请再次检查文件名。如果文件下载有一个字符是空格键(“”),例如:“template import.xls”。Firefox将出现错误,下载时没有扩展名。您可以使用函数replace(“,”)要解决此问题,请执行以下操作:


Response.AddHeader(“Content Disposition”、“attachment;filename=\”“+replace(dr[“filename”].ToString(),”“,”“,”);

此线程似乎遇到了与您应该尝试设置
ContentType
相同的问题,有时浏览器会忽略扩展名,而只使用
ContentType
\“after filename=应删除,或在dr[“filename”]后关闭。ToString()问题已解决伙计们,感谢您的输入。该线程似乎出现了与您应该尝试设置
ContentType
相同的问题。有时浏览器会忽略扩展名,只使用
ContentType
\“after filename=应在dr[“filename”]之后删除或关闭。ToString()问题解决了,伙计们,谢谢你的输入是的,你认为它会保存为html文档是正确的,它会对除office文件外的所有文件执行此操作引号是我的罪魁祸首谢谢!是的,你认为它会保存为html文档是正确的,它会对除office文件外的所有文件执行此操作引号是我的罪魁祸首。。谢谢