在asp.net C#应用程序的浏览器之间丢失的文件扩展名 [英] file extensions lost between browsers in asp.net c# application

查看:127
本文介绍了在asp.net C#应用程序的浏览器之间丢失的文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对你们中的一些技术问题。

基本上我一直在测试我主要是在Firefox的应用程序,我在应用程序下载功能,用户可以从SQL Server数据库文件。问题是,当我下载的文件在Internet Explorer中的每个文件除了将失去其扩展名从Microsoft文件例如一句话,访问等,有在firefox没有问题,除了位图...

这是我的code和谢谢

  //下载附件文件
    保护无效AttachmentDLBut_Click(对象发件人,EventArgs的发送)
    {
        //设置ID为所选行
        VAR ID =的Request.QueryString [ID];
        使用(SqlConnection的康恩=新的SqlConnection(******))
        {
            字符串SQL =选择文件类型,文件名,说明从NetworkEquipment WHERE NetworkEquipmentID ='+身份证+';            使用(CMD的SqlCommand =新的SqlCommand(SQL,康涅狄格州))
            {
                //cmd.Parameters.AddWithValue(\"@ID,AttachmentDDL.SelectedItem.Value);
                conn.Open();                SqlDataReader的博士= cmd.ExecuteReader();                尝试
                {
                    //如果有附件...下载
                    如果(dr.Read())
                    {
                        Response.Clear();
                        Response.ContentType =博士[文件类型]的ToString()。
                        Response.AddHeader(内容处置,附件;文件名= \\+博士[文件名]的ToString());
                        Response.BinaryWrite((字节[])博士[说明]);
                        到Response.End();                        conn.Close();
                    }
                }
                赶上(SQLEXCEPTION sqlex){MessageBox.Show(对不起,已经在下载该文件时出现了意外的错误:+ sqlex); }
                赶上(例外前){MessageBox.Show(对不起,发生意外的错误,同时下载该文件时出现错误:+ EX); }                //别的什么也没有发生
            }
        }
    }


解决方案

使用F12开发人员工具或萤火虫或类似的,看看有什么实际上是由服务器发送。在这种情况下,它会是这样的:

 内容处置:附件;文件名=FILENAME.EXT

该服务器还可以缺席发送此或类似的东西:

 的Content-Type:text / html的

首先,你需要的文件名后关闭报价。还建议分号之后的空格。您还需要清理的文件名,以确保它不包含混淆或非法字符。至少,你需要剥离双引号或引用他们。

其次,你需要添加一个Content-Type头。作弊是设置

 内容类型:应用程序/八位字节流

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

I have a technical question for some of you.

Basically i have been testing my application primarily in firefox, I have a download feature in the application where the user can download a file from SQL server database. The problem is that when i download the file in Internet explorer every file will lose its extension apart from the Microsoft files e.g. word, access etc. There are no problems in firefox apart from bitmaps...

here is my code and thank you

    //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
            }
        }
    }

解决方案

Use F12 developer tools or FireBug or similar to see what is actually sent by the server. In this case it will be something like:

Content-Disposition: attachment;filename="filename.ext

The server may also send this or something similar by default:

Content-Type: text/html

Firstly, you need to close the quotes after filename. A space after the semicolon is also recommended. You also need to clean up the filename to make sure it doesn't contain confusing or illegal characters. At the very least you need to strip double quotes or quote them.

Secondly you need to add a Content-Type header. The cheat is to set

Content-Type: application/octet-stream 

which will result in the browser guessing based on the file extension.

这篇关于在asp.net C#应用程序的浏览器之间丢失的文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆