当前位置: 代码迷 >> ASP.NET >> asp.net 实现从数据库下载文件解决办法
  详细解决方案

asp.net 实现从数据库下载文件解决办法

热度:1011   发布时间:2013-02-25 00:00:00.0
asp.net 实现从数据库下载文件
问题:我把txt、doc等文件以二进制的形式存储到数据库,但是我在从数据库里下载后的文件却无法打开……
下载的方法在网上找到,如下:
context.Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(dr["CTitle"].ToString()));
context.Response.BinaryWrite((Byte[])dr["CContent"]);
本人小白一个,高手看不惯的勿喷!
求解惑——为什么无法打开?
或者请给我一个类似的例子让我自己琢磨,谢谢!

------解决方案--------------------------------------------------------
完整的例子
HTML code
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">  protected void Button1_Click(object sender, EventArgs e)  {    //得到文件数组    byte[] fileData = FileUpload1.FileBytes;    //得到文件大小    int fileLength = FileUpload1.PostedFile.ContentLength;    //得到文件名字    string fileName = System.IO.Path.GetFileName( FileUpload1.FileName);    //得到文件类型    string fileType = FileUpload1.PostedFile.ContentType;    //构建数据库连接,SQL语句,创建参数    string strCnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Image2Access.mdb";    System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(strCnString);    String strSql = "INSERT INTO FileTable (ContentType,Content,Title)" +                    "VALUES (@ContentType,@Content,@Title)";    System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(strSql, myConnection);    command.Parameters.AddWithValue("@ContentType", fileType);       command.Parameters.AddWithValue("@Content", fileData);    command.Parameters.AddWithValue("@Title", fileName);      //打开连接,执行查询    myConnection.Open();    command.ExecuteNonQuery();    myConnection.Close();    Response.Redirect(Request.FilePath);  }  protected void Page_Load(object sender, EventArgs e)  {       //构建数据库连接,SQL语句,创建参数    string strCnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Image2Access.mdb";    System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(strCnString);    String strSql = "select * from FileTable";    System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(strSql, myConnection);    //打开连接,执行查询    myConnection.Open();    System.Data.OleDb.OleDbDataReader dr = command.ExecuteReader();    GridView1.DataSource = dr;    GridView1.DataBind();        myConnection.Close();  }</script><html xmlns="http://www.w3.org/1999/xhtml"><head id="Head1" runat="server">  <title></title></head><body>  <form id="form1" runat="server">  <asp:FileUpload ID="FileUpload1" runat="server" />  <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传文件" />  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">  <Columns>  <asp:HyperLinkField DataNavigateUrlFields="FileId" DataTextField="Title" DataNavigateUrlFormatString="~/Download.aspx?FileId={0}"/>    </Columns>  </asp:GridView>  </form></body></html>
------解决方案--------------------------------------------------------
参考:
http://www.cnblogs.com/insus/articles/1411761
http://www.cnblogs.com/insus/articles/2003336
------解决方案--------------------------------------------------------
只能给你一点建议的,我的程序是用DELPHI+MSSQL2005编写的
数据库中的表是
字段名 FILENAME FILEDDD
类型 nvarchar(30) image
将文件内容以内存流方式写入FILEDDD中,读取时再以内存流方式读取并保存文件
名为FILENAME即可,再用外部的程序打开即可
  相关解决方案