473,378 Members | 1,498 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 969

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Prakash | last post by:
Hi, We face problems uploading excel (with macros) documents using HTML File Upload. The file contents are corrupted while viewing the same. However, we are able to upload excel (w/o. macros)...
4
by: sri2097 | last post by:
Hi all, I'm storing number of dictionary values into a file using the 'cPickle' module and then am retrieving it. The following is the code for it - # Code for storing the values in the file...
1
by: r1100r98 | last post by:
I am having a problem moving the data from a datatable to the SQL2005 table (using VB2005). See code below. The SQL2005 table is empty, the datatable is being filled from a text file, not from...
1
by: Roel | last post by:
Hi all, I'm using the FileUpload control for uploading MS Word and MS PowerPoint documents. To check if a user uploads a correct document, I check the contenttype...
5
by: djhexx | last post by:
Hello. I have an ASP.NET application (C#) that I allow users to upload files. Files get stored in a SQL2005 database. The file data is stored in a varbinary(max) column. When the user uploads...
2
by: asp.net | last post by:
hello gents, i have a similar thing to do. i am not much into .net yet. i am currently working on asp website which generates doc file on a fly. i now want to convert these doc to docx files. i am...
2
by: gwen.demby | last post by:
Hello, I hope all is well.. I was wondered if someone had a fix for opening docx file... I am currently using the response.redirect("path of word document") however it does not work while trying to...
2
by: =?Utf-8?B?TWFkaXNvbg==?= | last post by:
Hi there, I'm working with VS 2008(framework3.5), IIS6, SQL Server 2005. I have website that allow users to upload doc, docx, xls, xlsx ... and so on. I save file to SQL Server 2005 with...
4
by: =?Utf-8?B?U1VOTlk=?= | last post by:
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...
1
by: mamul | last post by:
Hi All, I am able to read and write to .txt, .bin files. but the same program is not support for .docx file . can someone help me the procedure to perform read write operation on .docx file. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.