Click here to Skip to main content
15,903,201 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all ,

I uploaded a file of type docx in asp.net and iam trying to download it using the following code :

VB
If e.CommandName = "Download" Then
           Dim fst As New FileStream(Server.MapPath(e.CommandArgument), FileMode.Open)
           Dim fl As String = e.CommandArgument
           Dim file As String() = e.CommandArgument.ToString.Split(".")
           Dim extension As String = file(file.Length - 1)
           Dim bytBytes(fst.Length) As Byte
           'read file byte by byte
           fst.Read(bytBytes, 0, fst.Length)
           fst.Close()
           Response.AddHeader("Content-disposition", "attachment; filename=" & fl)

           If extension = "docx" Then
               Response.ContentType = "application/vnd.ms-word.document"
           ElseIf extension = "pdf" Then
               Response.ContentType = "Application/pdf"
           ElseIf extension = "xls" Then
               Response.ContentType = "application/vnd.ms-excel"
           ElseIf extension = "xlsx" Then
               Response.ContentType = "application/vnd.ms-excel"
           ElseIf extension = "doc" Then
               Response.ContentType = "application/ms-word"
           End If


           'write all the byte to newly created file
           Response.BinaryWrite(bytBytes)
           Response.End()
       End If


All the other files except the docx are being downloaded and opened,but the docx file is downloaded and corrupted.So how to fix it ?

Thanks .
Posted
Comments
Bryian Tan 30-Apr-12 21:22pm    
Did you check if the file was corrupted during the upload process?
Mohamed Mitwalli 1-May-12 1:53am    
Can you check on file after uploaded if it's not corrupted .

change the response.Content type.

Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document 
 
Share this answer
 
Comments
mhamad zarif 7-Oct-11 3:15am    
it did not work.
Mohamed Mitwalli 1-May-12 1:54am    
maybe the problem in the File it self and it uploaded in wrong way .
 
Share this answer
 
This worked for me. Try out!!

string filePath = Server.MapPath("File/Test.docx"); // Specify the location of the .docx file
using (FileStream fileStream = File.OpenRead(filePath))
{
MemoryStream memStream = new MemoryStream();
memStream.SetLength(fileStream.Length);
fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);

Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
Response.AddHeader("Content-Disposition", "attachment; filename=myfile.docx");
Response.BinaryWrite(memStream.ToArray());
Response.Flush();
Response.Close();
Response.End();
}
 
Share this answer
 
v2
Comments
CHill60 27-Jun-13 14:47pm    
The question is over a year old and already has solutions posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900