如何将字节arry转换为其原始文件(下载文件时) [英] how to convert byte arry to its original file (when downloading file)
本文介绍了如何将字节arry转换为其原始文件(下载文件时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我开发了一个用于上传文件和下载文件的应用程序,
我使用数据库存储文档文件,文档存储在
字节数组,
现在,我无法将该字节数组转换回其原始文件格式.
任何人都可以帮助我解决这个问题吗?
我尝试了以下代码进行下载:
I Developed an application for Uploading Files and Downloading Files,
I used database to store my document file in which, Documents are stored in
Byte array,
Now i am unable to convert back that byte array into its original file format.
can any one help me to solve out this problem?
i tried following code for download:
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition",
"attachment; filename=" + fileName);
HttpContext.Current.Response.AddHeader("Content-Length",
Convert.ToString(value.Length));
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.Write(value);
HttpContext.Current.Response.End();}
help me if any one get any idea!!!
推荐答案
请勿使用Response.Write,而应使用Response.BinaryWrite.
我处理它的方法是使用ASPX文件:
Don''t use Response.Write, use Response.BinaryWrite instead.
The way I handle it is to use an 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.End();
%>
Hi OriginalGriff,
当我放入Response.BinaryWrite时,
它与Response.Write和
相同 再次给出错误的数据格式
并在用记事本打开时给出"System Bytes []"作为输出.
问候,
tushar
Hi OriginalGriff,
when i puts Response.BinaryWrite,
it works same as Response.Write and
again it give wrong format of data
and gives "System Bytes[]" as output when i opens it with notepad.
Regards,
tushar
我使用以下代码获得了此问题的解决方案:
I got Solution of this problem using following code:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + FileName + "\"");
HttpContext.Current.Response.AddHeader("Content-Length", Convert.ToString(Content.Length));
HttpContext.Current.Response.BinaryWrite(Content);
HttpContext.Current.Response.End();
我将文件的数据类型设置为二进制.
谢谢大家的帮助!
and i set data type of file to binary.
thank you all for help!!!
这篇关于如何将字节arry转换为其原始文件(下载文件时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文