469,347 Members | 1,691 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,347 developers. It's quick & easy.

DOCX FILE CORRUPTED WHEN RETRIEVED FROM SQL2005

Hi, i am uploading a .docx file into sql2005 and later when i retrieve the
file from the database and i open it, i get a error message "The file is
corrupted and cannot be open". I am not facing this issue when i store and
retrieve files of type .doc

i am storing the file in a column of datatype image.
Here is the code where i insert the file into database. Am sure the code is
working. its just not working for .docx

protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile && FileUpload1.PostedFile != null)
{
int ilength =
Convert.ToInt32(FileUpload1.PostedFile.ContentLeng th);
string content = FileUpload1.PostedFile.ContentType;
string file_name = FileUpload1.FileName;
Byte[] bytecontent = new Byte[ilength];

con = new SqlConnection();
con.ConnectionString = "Server=W2RZYFV603\\SQLEXPRESS;
Database=master; Trusted_Connection=True";
con.Open();

string sql = "insert into
files(file_data,name,content_type,file_size)VALUES (@file, @name, @content,
@size)";
try
{
SqlCommand cmd = new SqlCommand(sql, con);
FileUpload1.PostedFile.InputStream.Read(byteconten t, 0,
FileUpload1.PostedFile.ContentLength);
cmd.Parameters.AddWithValue("@file", bytecontent);
cmd.Parameters.AddWithValue("@name", file_name);
cmd.Parameters.AddWithValue("@content", content);
cmd.Parameters.AddWithValue("@size", ilength);
int i = cmd.ExecuteNonQuery();
TextBox1.Text = i.ToString();
}
catch (Exception ex)
{
TextBox1.Text = ex.Message;
}
finally
{
con.Close();
}

}
Here is the code to retrieve the data from the database.
protected void Page_Load(object sender, EventArgs e)
{
try
{

con = new SqlConnection();
con.ConnectionString = "Server=W2RZYFV603\\SQLEXPRESS;
Database=master; Trusted_Connection=True";
con.Open();

}
catch (Exception ex)
{

}
string qid = "select max(id) from files";
int id;
SqlCommand cmd1 = new SqlCommand(qid, con);
id = Convert.ToInt32( cmd1.ExecuteScalar());
string query = "select * from files where id="+ id.ToString();
//string query = "select * from images where id=4";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader dr = cmd.ExecuteReader();

if (dr.Read())
{
byte[] imagecontent = (byte[])(dr[1]);
Response.ContentType = dr[3].ToString();
Response.AddHeader("Content-Disposition",
"attachment;filename="+dr[2].ToString());
Context.Response.BinaryWrite(imagecontent);

}
cmd = null;
dr.Close();
con.Close();

}

Please let me know as to why is this not working for .docx files.
--
SUNNY
Aug 7 '08 #1
0 862

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by sri2097 | last post: by
1 post views Thread by Roel | last post: by
2 posts views Thread by asp.net | last post: by
2 posts views Thread by =?Utf-8?B?TWFkaXNvbg==?= | last post: by
4 posts views Thread by =?Utf-8?B?U1VOTlk=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.