从Oracle的Blob字段中下载文件

思路:从Blob字段中获取byte数组,然后将byte数组输出到客户端,注意设置网页的Header

代码如下:

 1 string constr = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

 2 string sqlstr = "select file_name,FILECONTENT from fjg_file_manage where numbers='" + FileNum + "'";

 3 OracleConnection conn = new OracleConnection(constr);

 4 OracleCommand cmd = new OracleCommand(sqlstr, conn);

 5 

 6 conn.Open();

 7 OracleDataReader dr = cmd.ExecuteReader();

 8 dr.Read();

 9 //由于文件名中有空格,下载后空格会变成加号(+),因此需要进行特殊处理

10 string fileName = HttpUtility.UrlEncode(fileName,Encoding.UTF8);   //编码后空格会变成加号

11 fileName = fileName.Replace("+", "%20"); //将加号替换为空格

12 //一定要设置好header,不然网页中不会弹出下载框

13 HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);

14 HttpContext.Current.Response.ContentType = "application/octet-stream";

15 context.Response.BinaryWrite((byte[])dr["FILECONTENT"]);

16 conn.Close();

你可能感兴趣的:(oracle)