By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,594 Members | 3,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,594 IT Pros & Developers. It's quick & easy.

DOCX FILE CORRUPTED WHEN RETRIEVED FROM SQL2005

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.