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(o bject sender, EventArgs e)
{
if (FileUpload1.Ha sFile && FileUpload1.Pos tedFile != null)
{
int ilength =
Convert.ToInt32 (FileUpload1.Po stedFile.Conten tLength);
string content = FileUpload1.Pos tedFile.Content Type;
string file_name = FileUpload1.Fil eName;
Byte[] bytecontent = new Byte[ilength];
con = new SqlConnection() ;
con.ConnectionS tring = "Server=W2RZYFV 603\\SQLEXPRESS ;
Database=master ; Trusted_Connect ion=True";
con.Open();
string sql = "insert into
files(file_data ,name,content_t ype,file_size)V ALUES(@file, @name, @content,
@size)";
try
{
SqlCommand cmd = new SqlCommand(sql, con);
FileUpload1.Pos tedFile.InputSt ream.Read(bytec ontent, 0,
FileUpload1.Pos tedFile.Content Length);
cmd.Parameters. AddWithValue("@ file", bytecontent);
cmd.Parameters. AddWithValue("@ name", file_name);
cmd.Parameters. AddWithValue("@ content", content);
cmd.Parameters. AddWithValue("@ size", ilength);
int i = cmd.ExecuteNonQ uery();
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(objec t sender, EventArgs e)
{
try
{
con = new SqlConnection() ;
con.ConnectionS tring = "Server=W2RZYFV 603\\SQLEXPRESS ;
Database=master ; Trusted_Connect ion=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.ExecuteSca lar());
string query = "select * from files where id="+ id.ToString();
//string query = "select * from images where id=4";
SqlCommand cmd = new SqlCommand(quer y, con);
SqlDataReader dr = cmd.ExecuteRead er();
if (dr.Read())
{
byte[] imagecontent = (byte[])(dr[1]);
Response.Conten tType = dr[3].ToString();
Response.AddHea der("Content-Disposition",
"attachment;fil ename="+dr[2].ToString());
Context.Respons e.BinaryWrite(i magecontent);
}
cmd = null;
dr.Close();
con.Close();
}
Please let me know as to why is this not working for .docx files.
--
SUNNY 6 4639
Well, one thought would be: are you sure that there are no artefacts
before/after the BLOB? I would recommend using a handler (not an aspx)
for download pages so you know exactly what is in the stream. It might
be that Word is OK at ignoring rubbing in the binary, but a docx might
appear malformed... equally, it would be wise to set things like the
content-length.
It might also depend on the size of the file; I tend to use chunking for
reading/writing BLOBs, but this isn't as easy... but it would be worth
verifying how much data you are reading from the database vs how much is
there (datalength). There may be a limit to what SqlDataReader will read
in one hit, since an overly-big byte[] buffer is never a good thing...
Marc
Hi Marc,
the data type of column where i am storing the file is of type Image.
I checked and i see that the amount of data i am reading from the database
and the datalength is same.
The size of file i am using for testing purpose is just 25 kb, so it
shouldnt be a problem.
if i can get more help, it would be appreciated.
--
SUNNY
"Marc Gravell" wrote:
Well, one thought would be: are you sure that there are no artefacts
before/after the BLOB? I would recommend using a handler (not an aspx)
for download pages so you know exactly what is in the stream. It might
be that Word is OK at ignoring rubbing in the binary, but a docx might
appear malformed... equally, it would be wise to set things like the
content-length.
It might also depend on the size of the file; I tend to use chunking for
reading/writing BLOBs, but this isn't as easy... but it would be worth
verifying how much data you are reading from the database vs how much is
there (datalength). There may be a limit to what SqlDataReader will read
in one hit, since an overly-big byte[] buffer is never a good thing...
Marc
So the next step is to use something like Fiddler to see what *actually*
comes down the wire. If this is an aspx I wouldn't be in the least bit
surprised to see a lot of contamination. You can get aspx to render a
clean stream, but it is a lot simpler to just use a handler (or ashx) to
start with...
Marc
On Aug 7, 6:42*am, 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(o bject sender, EventArgs e)
* * {
* * * * if (FileUpload1.Ha sFile && FileUpload1.Pos tedFile != null)
* * * * {
* * * * * * int ilength =
Convert.ToInt32 (FileUpload1.Po stedFile.Conten tLength);
* * * * * * string content = FileUpload1.Pos tedFile.Content Type;
* * * * * * string file_name = FileUpload1.Fil eName;
* * * * * * Byte[] bytecontent = new Byte[ilength];
* * * * * * con = new SqlConnection() ;
* * * * * * con.ConnectionS tring = "Server=W2RZYFV 603\\SQLEXPRESS ;
Database=master ; Trusted_Connect ion=True";
* * * * * * con.Open();
* * * * * * string sql = "insert into
files(file_data ,name,content_t ype,file_size)V ALUES(@file, @name, @content,
@size)";
* * * * * * try
* * * * * * {
* * * * * * * * SqlCommand cmd = new SqlCommand(sql, con);
* * * * * * * * FileUpload1.Pos tedFile.InputSt ream.Read(bytec ontent, 0,
FileUpload1.Pos tedFile.Content Length);
* * * * * * * * cmd.Parameters. AddWithValue("@ file", bytecontent);
* * * * * * * * cmd.Parameters. AddWithValue("@ name", file_name);
* * * * * * * * cmd.Parameters. AddWithValue("@ content", content);
* * * * * * * * cmd.Parameters. AddWithValue("@ size", ilength);
* * * * * * * * int i = cmd.ExecuteNonQ uery();
* * * * * * * * 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(objec t sender, EventArgs e)
* * {
* * * * try
* * * * {
* * * * * * con = new SqlConnection() ;
* * * * * * con.ConnectionS tring = "Server=W2RZYFV 603\\SQLEXPRESS ;
Database=master ; Trusted_Connect ion=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.ExecuteSca lar());
* * * * string query = "select * from files where id="+ id.ToString();
* * * * //string query = "select * from images where id=4";
* * * * SqlCommand cmd = new SqlCommand(quer y, con);
* * * * SqlDataReader dr = cmd.ExecuteRead er();
* * * * if (dr.Read())
* * * * {
* * * * * * byte[] imagecontent = (byte[])(dr[1]);
* * * * * * Response.Conten tType = dr[3].ToString();
* * * * * * Response.AddHea der("Content-Disposition",
"attachment;fil ename="+dr[2].ToString());
* * * * * * Context.Respons e.BinaryWrite(i magecontent);
* * * * }
* * * * cmd = null;
* * * * dr.Close();
* * * * con.Close();
* * }
Please let me know as to why is this not working for .docx files.
--
SUNNY
Hi,
I would recommend you to use a console or a win test prog. to see if
the file is going out ok from the db
<=?Utf-8?B?U1VOTlk=?= <SUNNY>wrote:
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
Aside from anything else, you're assuming that you'll be able to read
the whole of the data from the web request in a single call to Read.
Never a good idea.
See http://pobox.com/~skeet/csharp/readbinary.html
--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
SUNNY wrote:
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
Since the code treats the files as bytes, then it is obviously not .doc
versus .docx that makes the difference, since bytes is bytes no matter
what content.
You need to look for other differences.
int ilength =
Convert.ToInt32 (FileUpload1.Po stedFile.Conten tLength);
string content = FileUpload1.Pos tedFile.Content Type;
string file_name = FileUpload1.Fil eName;
Byte[] bytecontent = new Byte[ilength];
con = new SqlConnection() ;
con.ConnectionS tring = "Server=W2RZYFV 603\\SQLEXPRESS ;
Database=master ; Trusted_Connect ion=True";
con.Open();
string sql = "insert into
files(file_data ,name,content_t ype,file_size)V ALUES(@file, @name, @content,
@size)";
try
{
SqlCommand cmd = new SqlCommand(sql, con);
FileUpload1.Pos tedFile.InputSt ream.Read(bytec ontent, 0,
FileUpload1.Pos tedFile.Content Length);
I would check the number of bytes actually read here !
In fact you should really read in a while loop.
cmd.Parameters. AddWithValue("@ file", bytecontent);
cmd.Parameters. AddWithValue("@ name", file_name);
cmd.Parameters. AddWithValue("@ content", content);
cmd.Parameters. AddWithValue("@ size", ilength);
int i = cmd.ExecuteNonQ uery();
con = new SqlConnection() ;
con.ConnectionS tring = "Server=W2RZYFV 603\\SQLEXPRESS ;
Database=master ; Trusted_Connect ion=True";
con.Open();
string qid = "select max(id) from files";
If you used the same connection, then you could just use
SCOPE_IDENTITY( ).
I know this is just test, but SELECT MAX(id) is a very bad habit.
int id;
SqlCommand cmd1 = new SqlCommand(qid, con);
id = Convert.ToInt32 ( cmd1.ExecuteSca lar());
string query = "select * from files where id="+ id.ToString();
//string query = "select * from images where id=4";
SqlCommand cmd = new SqlCommand(quer y, con);
SqlDataReader dr = cmd.ExecuteRead er();
if (dr.Read())
{
byte[] imagecontent = (byte[])(dr[1]);
Response.Conten tType = dr[3].ToString();
Response.AddHea der("Content-Disposition",
"attachment;fil ename="+dr[2].ToString());
Context.Respons e.BinaryWrite(i magecontent);
You should test imagecontent.Le ngth here and compare:
* files actual size
* size when inserted
* size when retrieved
Arne This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 the SQL2005 table.
I have tried various ways, but the SQL table is not updating. Help
would be appreciated.
Code is below
Thanks
Imports System
Imports System.Data
|
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 the file, I store it in a database. When a user
requests to download a file, the file is retrieved from the database
and then sent to them.
The files go in there ok. This is how I store them:
|
by: nitupatra |
last post by:
I have perl script which will make a zip file and create a HTML form with asubmit button to send the zip file client. When user click the download button the zip file will be downloaded. But the problem is now the downloaded zip file is corrupted.
here the scripts i wrote.
To create the Zip file.
#!/perl/bin/Perl.exe
use CGI;
use Archive::Zip qw( :ERROR_CODES );
|
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 open docx
files.. I recieve an "HTTP Error 404 - File or directory not found.
Internet Information Services (IIS)" error...
thanks for your help.
|
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 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...
| |
by: mamul |
last post by:
Hi can some one help me how to read a .docx file and stored into memory in c++.
Thanks
|
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.
please give me some exmple or if u have solve this type of problem in c++ then please reply me.
Thanks,
Mamul
|
by: Zabivb |
last post by:
I'm getting problem while retrieving the saved docx file from database, Its opening as zip file, I can able to open other format such as .txt,.jpg, .doc but i cant able to open .docx file... can some one help me regarding this.
Thanks
zabi
|
by: Zabivb |
last post by:
Hi
I'm facing problem while opening .docx file, apart from .docx file other files such as .doc,.jpg,.gif etc is opening fine! The only file which i cant able to open is .docx file. Can some one give solutions to solve this.
Coding for refrence
if (($extension1 != "doc") && ($extension1 != "docx") {
echo '<h4 class ="Alert">Unknown extension!</h4>';
}
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |