473,408 Members | 1,702 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,408 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
4 4229
On Aug 7, 12:47*pm, SUNNY <SUNNYwrote:
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
Ensure that aspx file has no html layout "<html><head>....."

Add

context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.ContentType = "application/vnd.ms-word.document.12";

and see if it helps
Aug 7 '08 #2
What Alexey said.

As a side comment, why are you storing documents in the master database ?

Database=master

The master database is a system database which should only be used for system information.
Create a proper database and use *that* to store documents in.

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"Alexey Smirnov" <al************@gmail.comwrote in message
news:2c**********************************@c65g2000 hsa.googlegroups.com...
On Aug 7, 12:47 pm, SUNNY <SUNNYwrote:
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
Ensure that aspx file has no html layout "<html><head>....."

Add

context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.ContentType = "application/vnd.ms-word.document.12";

and see if it helps
Aug 7 '08 #3
I am not really answering, as much as clarifying the why. Both Alexy and
Juan have done a good job of aid.

First, let's look at the corrupted document issue: .DOCX is a zip file, not
a document, like .DOC. Want proof? Change the extension to .ZIP and open it.
In side you will find the following directories and files (the # indicates
there may be more of each of these types of files):

_Rels
.rels
customXml
_rels
.rels
item#.xml
itemProps#.xml
docProps
app.xml
core.xml
word
_rels
document.xml.rels
theme
theme#.xml
--various docs here
[Content_Types].xml

Interesting, eh? This explains why Alexy wants you to remove the headers.
Note also that you cannot just display the contents of a .docx file in a
page (as part of the page, not the whole, for example), as it is not a
document in the traditional sense.

As for Juan's comment, I cannot state strongly enough why you should not put
your own data in the master database. If you end up corrupting anything, you
will likely end up with a database that is unreverable, as many of the
recovery bits are in the master database. Not wise.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

********************************************
| Think outside the box! |
********************************************
<SUNNYwrote in message
news:75**********************************@microsof t.com...
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 8 '08 #4
"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamMwrote in
message news:OF****************@TK2MSFTNGP04.phx.gbl...
>I am not really answering, as much as clarifying the why. Both Alexy and
Juan have done a good job of aid.
Indeed.
First, let's look at the corrupted document issue: .DOCX is a zip file,
not a document, like .DOC. Want proof? Change the extension to .ZIP and
open it. In side you will find the following directories and files (the #
indicates there may be more of each of these types of files):
Same with most of the Office 2007 file formats e.g. xlsx, pptx etc...
As for Juan's comment, I cannot state strongly enough why you should not
put your own data in the master database. If you end up corrupting
anything, you will likely end up with a database that is unreverable, as
many of the recovery bits are in the master database. Not wise.
Not only that - if the master database itself becomes corrupt, you may end
up having to rebuild the entire SQL Server...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 8 '08 #5

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: 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...
6
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. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.