从SQL Server数据库下载.pdf [英] Download .pdf from SQL Server database
本文介绍了从SQL Server数据库下载.pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我正在开发一个Web应用程序.在此应用程序中,用户可以上载和下载.pdf文件,并以二进制形式存储在Sql Server中.我在gridview中显示文件列表.当用户单击gridview中的下载按钮时,可以在客户端下载文件.
有人可以帮帮我吗.
谢谢
bandroid
Hi
I am developing a web application. In this application a user can upload and download a file .pdf and stored in Sql Server as binary.I show the list of files in a gridview. When user clicks on a download button in gridview it can be downloaded the file on client side.
Can some one help me.
thanks
bandroid
推荐答案
我这样做:在我的页面中有一个表,我在其中添加了数据库中所有相关的文件名.
然后,ASPX控件根据数据库中的行索引为我处理下载.
页面加载事件处理程序:
I do it like this: I have a table in my page, to which I add all the relevant file names from my DB.
An ASPX control then handles the download for me, based on the row index from the DB.
Page Load event handler:
Dictionary<string, Guid> downloads = GetDownloadList();
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, Guid> file in downloads)
{
sb.Append("<tr><td>");
sb.Append(file.Key);
sb.Append("</td></tr>");
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
cell.InnerHtml = "<a href=\"wm5ftdl.aspx?file=" + file.Value + "\" target=\"_blank\">" + file.Key + "</a>";
row.Cells.Add(cell);
tbDownloads.Rows.Add(row);
}
wm5ftdl.ASPX文件:
wm5ftdl.ASPX file:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="wm5ftdl.aspx.cs" Inherits="wm5ftdl" %>
<%
// Send a download file to the client given the filename.
string guid = Request.QueryString["file"];
string fileName = "ERROR";
byte[] data = new byte[] { 0, 0, 0, 0 };
string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DownloadDatabase"].ConnectionString;
using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon))
{
con.Open();
string strcmd = "SELECT [iD] ,cn.[fileName],[description] ,[dataContent] ,[version] " +
"FROM dlContent cn " +
"WHERE cn.iD=@ID";
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(strcmd, con))
{
cmd.Parameters.AddWithValue("@ID", guid);
using (System.Data.SqlClient.SqlDataReader r = cmd.ExecuteReader())
{
if (r.Read())
{
fileName = (string) r["filename"];
data = (byte[]) r["dataContent"];
}
}
}
}
Response.Clear();
Response.AddHeader("Cache-Control", "no-cache, must-revalidate, post-check=0, pre-check=0");
Response.AddHeader("Pragma", "no-cache");
Response.AddHeader("Content-Description", "File Download");
Response.AddHeader("Content-Type", "application/force-download");
Response.AddHeader("Content-Transfer-Encoding", "binary\n");
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.BinaryWrite(data);
//Response.WriteFile("wm5ftdata/" + fileName);
Response.End();
%>
希望此链接对您有所帮助" ^ ]
"Hope this link will help u" http://imar.spaanjaars.com/414/storing-uploaded-files-in-a-database-or-in-the-file-system-with-aspnet-20[^]
这篇关于从SQL Server数据库下载.pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文