回答

收藏

在ASP.NET C#应用程序的浏览器之间丢失的文件扩展名

技术问答 技术问答 249 人阅读 | 0 人回复 | 2023-09-14

我对你们中的一些人有技术问题。
" Z5 W: g* ?9 \/ c. s3 X基本上,我主要在firefox中测试过我的应用程序,我在应用程序中具有下载功能,用户可以从其中下载SQL
+ T7 R- ?! c2 s1 ?0 V8 J$ Z1 t& iServer数据库中的文件。问题是,当我在Internet
0 S' }. b) |) E/ p% vExplorer中下载文件时,除Microsoft文件(如单词,访问权限等)外,每个文件都将失去其扩展名。firefox中除位图外没有任何问题…3 ]$ R/ f5 I. g( G6 j# ~0 b
这是我的代码,谢谢; ~- G9 e9 c8 F0 _. u6 H% M
    //Download attachment file
% v! v+ j3 v' c$ C    protected void AttachmentDLBut_Click(object sender, EventArgs e)' ~+ F, `; R1 U" A
    {
; P2 v9 v9 D* x" Y  C2 x        //set the ID for the selected row. O; b2 o4 I3 |5 u8 l5 O
        var ID = Request.QueryString["Id"];
% z, _' f( A, T* f: \" v/ B9 ]        using (SqlConnection conn = new SqlConnection("******"))
# e$ i5 B/ g; y! ~        {
% p+ @2 U- c+ ^6 H! }0 Z            string sql = "SELECT FileType, Filename, Description FROM NetworkEquipment WHERE NetworkEquipmentID = '" + ID + "'";" h/ E1 s) G. D  P+ E
            using (SqlCommand cmd = new SqlCommand(sql, conn))6 X. g* B* l8 n, U, U" `. u
            {
+ Z/ x! d1 f; r  R" l9 \                //cmd.Parameters.AddWithValue("@ID", AttachmentDDL.SelectedItem.Value);
: w/ y4 h$ k& Z) `                conn.Open();
3 e) j! R  T; T2 `1 p0 h                SqlDataReader dr = cmd.ExecuteReader();
. A) _, @* [1 L. G) b) B8 _2 K                try
( O# N/ A) @$ k                {; |9 _0 K2 @8 U$ }
                    //if there is an attachment... download it
3 O9 |( A8 H3 a" `                    if (dr.Read())" R/ v8 _) F0 l/ s, k7 y
                    {1 G% M9 j4 O! \6 w7 @: m2 s8 [
                        Response.Clear();
8 P  W* l' v7 o( [+ d                        Response.ContentType = dr["FileType"].ToString();# `5 t( x/ ?- f! u
                        Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["Filename"].ToString());5 A) a5 t5 x+ Z. H" J2 j- ]
                        Response.BinaryWrite((byte[])dr["Description"]);
. P% @# C0 W! \; r                        Response.End();) p+ J& P! g8 o/ Y* z  T1 {& Q& v
                        conn.Close();
4 N. n* `, E7 ]% U5 a- j                    }
3 r) d& N. V$ d2 A, k                }
3 d# w& `$ x; ]# ^' \2 }4 G                catch (SqlException sqlex) { MessageBox.Show("Sorry, an unexpected error has occurred while downloading this file. Error: " + sqlex); }
0 z9 i4 ]7 P9 A% W( k                catch (Exception ex) { MessageBox.Show("Sorry, an unexpected error has occurred while downloading this file. Error: " + ex); }3 e% N# Q) A( `
                //else nothing happens
. [* W1 r3 T) B' x) W  x, G            }
4 U. ]- _# t) K' H        }
; @1 m+ B! c8 K4 T* [) J  G, j$ D    }2 N! R  N1 R( I3 O$ _
               
% H& N- X2 \! L2 P8 M解决方案:
' o0 n8 P* H2 \7 N" a& G                4 h" H5 t; m/ _
" b/ A( C4 e. Q. n  [

3 _) E  ]6 l5 p6 u+ X6 u. y                使用F12开发人员工具或FireBug或类似工具查看服务器实际发送的内容。在这种情况下,它将类似于:
! ]3 @' P0 f& d4 g2 Q, v4 U- hContent-Disposition: attachment;filename="filename.ext* [: ]) o. _9 ]& }9 z& }
默认情况下,服务器也可能发送此消息或类似内容:! X! G' b  x9 `5 ?
Content-Type: text/html
2 ^# O0 T7 U) W首先,您需要关闭文件名后的引号。还建议在分号后留一个空格。您还需要清理文件名,以确保它不包含混乱或非法字符。至少您需要去除双引号或将其引号。# z( I5 D5 o* J7 _4 j/ g
其次,您需要添加一个Content-Type标头。作弊要定5 J0 M2 x. O! r9 k3 M, S, b+ h" s
Content-Type: application/octet-stream4 s, A$ w8 Z# r4 A7 S
这将导致浏览器根据文件扩展名进行猜测。
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则