文件以二进制流保存到数据库及下载(转载)
2008-08-21 14:40
639 查看
文:黎承湘
为了实现此功能,查阅不少资料,发过不少求助信息,现整理出来,以供有需人士查阅.
表名: myfile; 表字段: id(int), Content(image),Type(varchar(50))
注:这是请教一个达人找到的。在网上四处找寻把音频文件上传到数据库,然后读取出来播放,一直没结果。
特放上做为记录
为了实现此功能,查阅不少资料,发过不少求助信息,现整理出来,以供有需人士查阅.
表名: myfile; 表字段: id(int), Content(image),Type(varchar(50))
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.IO; public partial class Image2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SqlConnection cn = new SqlConnection();//数据库链接 cn.Open(); SqlDataAdapter da2 = new SqlDataAdapter( "select * from myfile where id= 24 ", cn);//读出数据库中相应的数据 DataSet ds2 = new DataSet(); da2.Fill(ds2); byte[] b2 = (byte[])ds2.Tables[0].Rows[0][ "files "]; string type = (string)ds2.Tables[0].Rows[0][ "type "]; Response.Clear(); string Type = checktype(type); Response.AddHeader("Content-Disposition","attachment; filename=”下载”+type); Response.AddHeader( "Content-Length ", b2.Length.ToString()); Response.ContentType = Type; Response.BinaryWrite(b2); Response.End(); string FileName = ((LinkButton)sender).CommandArgument; Response.Clear(); Response.ContentType = Type; Response.AddHeader("Content-Disposition","attachment;FileName= " + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)); Response.WriteFile(FileName); Response.End(); } //根据文件的扩展名来获取对应的“输出流的HTTP MIME“类型 private string checktype(string filename) { string ContentType; switch(filename.Substring(filename.LastIndexOf(". ")).Trim().ToLower()) { case ".asf ": ContentType = "video/x-ms-asf "; break; case ".avi ": ContentType = "video/avi "; break; case ".doc ": ContentType = "application/msword "; break; case ".zip ": ContentType = "application/zip "; break; case ".xls ": ContentType = "application/vnd.ms-excel "; break; case ".gif ": ContentType = "image/gif "; break; case ".jpg ": ContentType = "image/jpeg "; break; case "jpeg ": ContentType = "image/jpeg "; break; case ".wav ": ContentType = "audio/wav "; break; case ".mp3 ": ContentType = "audio/mpeg3 "; break; case ".mpg ": ContentType = "video/mpeg "; break; case ".mepg ": ContentType = "video/mpeg "; break; case ".rtf ": ContentType = "application/rtf "; break; case ".html ": ContentType = "text/html "; break; case ".htm ": ContentType = "text/html "; break; case ".txt ": ContentType = "text/plain "; break; default: ContentType = "application/octet-stream "; break; } return ContentType; } }
注:这是请教一个达人找到的。在网上四处找寻把音频文件上传到数据库,然后读取出来播放,一直没结果。
特放上做为记录
相关文章推荐
- 将文件上传、下载(以二进制流保存到数据库)实现代码
- C#将文件上传、下载(以二进制流保存到数据库)
- 将文件上传、下载(以二进制流保存到数据库)
- 将文件上传、下载(以二进制流保存到数据库)实现代码
- 文件以二进制流保存到数据库及下载
- 将文件上传、下载(以二进制流保存到数据库)
- WPF用流的方式上传/显示/下载图片文件(保存在数据库)
- struts2文件下载(文件保存以BLOB类型在数据库中)
- WPF用流的方式上传/显示/下载图片文件(保存在数据库)
- 生成/读取(反向更新数据库) Excel文件(示例代码下载) ----转载
- SilverLight C#程序之:把文件转为二进制流保存到数据库(任意文件类型)
- 艾伟_转载:下载文件时根据MIME类型自动判断保存文件的扩展名
- vs.net中打开数据库中的文件(保存/下载)
- MVC 保存二进制到数据库,并显示文件的方法(图片显示或下载文件)
- 把文件以二进制格式或base64字符串形式保存到数据库中,并实现下载功能源码
- 保存pdf文件至数据库,在线预览并下载到本地
- MFC通过URL下载并保存文件代码 转载
- 可以让保存在数据库中的文件查出 并被下载
- struts2文件下载(文件保存以BLOB类型在数据库中)
- 用SQL语句定时自动备份SQL SERVER 数据库并将备份文件加密压缩自动下载 (2014-03-17 13:12:26)转载