通过asp.net Web应用程序2005打开/下载文件(sql 2005二进制字段) [英] open / download file ( sql 2005 binary field) through asp.net web application 2005
本文介绍了通过asp.net Web应用程序2005打开/下载文件(sql 2005二进制字段)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用asp.net Web应用程序2005
我有sql2005二进制字段,并存储在.doc,.pdf,.xls .... files
我想打开/下载这些文件.
给我一个整体的例子.
谢谢您
Hi i am using asp.net web application 2005
i have sql2005 binary field and stored in .doc, .pdf, .xls....files
i would like to open/download those files.
give me a total example.
Thanking you
推荐答案
请在下面的链接中查看我的解决方案.
我已经说明了如何上传和下载文件.我还说明了用于执行我的解决方案的表结构.
http: //www.c-sharpcorner.com/Forums/Thread/179462/how-to-retraive-pdf-file-from-sql-server-please-give-pro.aspx [
Please check my solution in the below link.
I have explained how to upload and download files.i have explained table structure also for executing my solution.
http://www.c-sharpcorner.com/Forums/Thread/179462/how-to-retraive-pdf-file-from-sql-server-please-give-pro.aspx[^]
参考:
ContentType字符串,以便它指定适当的文件格式.该字符串的语法通常设置为类型/子类型",其中类型"是常规内容类别,子类型"是特定内容类型.有关受支持的内容类型的完整列表,请参阅Web浏览器文档或当前的HTTP规范.以下列表概述了一些常见的ContentType值:
文本/HTML"
图像/GIF"
图像/JPEG"
文字/纯文字"
应用程序/msword"(用于Microsoft Word文件)
应用程序/x-msexcel"(用于Microsoft Excel文件)
Ref:
ContentType string so that it specifies the appropriate file format. The syntax of this string is usually formatted as "type/subtype," where "type" is the general content category and "subtype" is the specific content type. For a full list of supported content types, refer to your Web browser documentation or the current HTTP specification. The following list outlines some common ContentType values:
"text/HTML"
"image/GIF"
"image/JPEG"
"text/plain"
"Application/msword" (for Microsoft Word files)
"Application/x-msexcel" (for Microsoft Excel files)
public DownloadContent(string ID)
{
SqlConnection con;
try
{
con = new SqlConnection();
con.ConnectionString = "YourConn";
con.Open();
string query = "your query to retrieve from db where ID = ID"// assuming 1 record here";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
byte[] imagecontent = (byte[])(dr[1]); //assuming second record on dr is the file bytes
Response.ContentType = "Application/msword"//sent your content type here, see ref link
Response.AddHeader("Content-Disposition", "attachment;filename=somename.doc");//set file type here.
Context.Response.BinaryWrite(imagecontent);
}
cmd = null;
dr.Close();
con.Close();
}
catch (Exception ex)
{
}
finally
{
if(con!= null)
con.Close();
}
}
try
{
DataSet ds1 = new DataSet();
GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);
//ReportID = Convert.ToInt32(gvAssesReport.DataKeys[gvr.RowIndex].Values[1]);
FileName = gvReport.DataKeys[gvr.RowIndex].Values[0].ToString();
int ReportID = Convert.ToInt16(gvReport.DataKeys[gvr.RowIndex].Values[1].ToString());
if (string.IsNullOrEmpty(FileName))
{
lblMessage.Text = "File not found";
}
else
{
if (File.Exists(Server.MapPath(Constants.Report_SavePath.ToString() + "//" + ReportID + "//" + FileName)))
{
Response.Clear();
Response.AppendHeader("content-disposition", "attachment; filename=" + FileName);
Response.ContentType = "application/Ms-Word";
lblMessage.Text = string.Empty;
Response.TransmitFile(Server.MapPath(Constants.Report_DisplayPath.ToString() + "//" + ReportID + "//" + FileName));
Response.Flush();
}
else
{
lblMessage.Text = "File not found";
}
}
}
catch (Exception ex)
{
}
finally
{
Response.End();
}
这篇关于通过asp.net Web应用程序2005打开/下载文件(sql 2005二进制字段)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文