Hi,
I am using ASP.NET 2.0 and c#.
I am uploading a word document into sql server 2005. I am converting the document to a base64string and then passing the value to database. I am able to upload the document. But I am not able to retrieve the document back.
The below code is used for uploading document:
Code:
if (DocUpload.PostedFile != null || (!String.IsNullOrEmpty(DocUpload.PostedFile.FileName)) || DocUpload.PostedFile.InputStream != null)
{
byte[] imageBytes = new byte[DocUpload.PostedFile.InputStream.Length + 1];
DocUpload.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length);
string DocFileName = DocUpload.PostedFile.FileName.ToString();
string DocContenttype = DocUpload.PostedFile.ContentType;
int DocLength = DocUpload.PostedFile.ContentLength;
// Convert the binary input into Base64 UUEncoded output.
string base64String;
base64String = System.Convert.ToBase64String(imageBytes, 0, imageBytes.Length);
InsertCommand.Parameters.Add(new SqlParameter("@MyDoc", SqlDbType.NText));
InsertCommand.Parameters["@MyDoc"].Value = base64String.ToString();
}
I am trying to retrieve the document using the below code:
Code:
protected void Button1_Click(object sender, EventArgs e)
{
string strQuery = "select Doc_Name,Doc_ContentType,My_DocData from tbl_tablename where ID=@ID'";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.Add("@ID", SqlDbType.BigInt).Value = 11;
DataTable dt = GetData(cmd);
if (dt != null)
{
download(dt);
}
}
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
private void download(DataTable dt)
{
byte[] bytes = ConvertStringToByteArray(dt.Rows[0]["My_DocData"].ToString());
Response.Clear();
Response.Charset = "utf-8";
Response.AddHeader("Accept-Header", bytes.Length.ToString());
Response.ContentType = dt.Rows[0]["Doc_ContentType"].ToString();
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Accept-Ranges", "bytes");
Response.Buffer = true;
Response.AddHeader("Expires", "0");
Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
Response.AddHeader("Pragma", "public");
Response.AddHeader("content-Transfer-Encoding", "binary");
Response.AddHeader("content-disposition", "attachment;filename="+ dt.Rows[0]["Doc_Name"].ToString() ;
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
public byte[] ConvertStringToByteArray(string input)
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
return enc.GetBytes(input);
}
But in the word document, which is getting generated, I am getting the string bytes, rather than the exact document.
How to retrieve the word document from database using c# 2.0?
Thank you