首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用通用http处理程序在C#中下载随机文件类型的blob

如何使用通用http处理程序在C#中下载随机文件类型的blob
EN

Stack Overflow用户
提问于 2013-05-08 01:26:43
回答 1查看 2.5K关注 0票数 0

我的困境是,我需要检索存储在oracle表中的blob数据。我使用jquery创建了与数据的链接,这些数据链接到一个通用的http处理程序blobHandler.ashx,我在其中查询数据库中的文件并将其返回给用户。这适用于在浏览器中打开的图像,但当涉及到其他文件类型(pdf、word、excel)时,程序仅下载.ashx文件本身。

如何让它下载带有其扩展名的文件?

代码语言:javascript
复制
using (OracleConnection objConn = new OracleConnection(conStr))
        {
            using (OracleCommand cmd = new OracleCommand())
            {
                cmd.Connection = objConn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "SELECT fl.file_content_type, fl.file_data,fl.file_name FROM fnd_lobs fl WHERE fl.file_id = " + mediano;

                try
                {
                    objConn.Open();
                    OracleDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {

                            OracleBlob blob = dr.GetOracleBlob(1);
                            FileStream FS = new FileStream(dr["file_name"].ToString(), FileMode.Create);
                            Byte[] byteArr = new Byte[blob.Length];
                            int i = blob.Read(byteArr,0,System.Convert.ToInt32(blob.Length));
                            MemoryStream memStream = new MemoryStream(byteArr);

                            context.Response.AddHeader("Content-disposition: inline", "attachment; filename=" + dr["file_name"]);
                            context.Response.ContentType = dr["file_content_type"].ToString();
                            context.Response.OutputStream.Write(byteArr,0,i);



                    }
                    }
                }
                catch(Exception exe)
                {
                    context.Response.Write(exe.Message);
                }
                finally
                {

                    cmd.Dispose();
                    //pcur.Dispose();
                    objConn.Close();
                    objConn.Dispose();
                    context.Response.Flush();
                    context.Response.End();
                }

            }
        }

我使用的是.net Framework1.1,visual studio 2003,oracle数据库版本10.3.0.5。

预先感谢您提供的任何建议这是我在这里的第一个问题。

EN

回答 1

Stack Overflow用户

发布于 2013-05-08 02:07:24

您需要在响应对象中设置正确的内容类型标头,以便浏览器知道如何相应地处理响应。

如果你想设置下载文件的名称,你可以查看here

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16425042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档