Click here to Skip to main content
15,912,504 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
not able to download docx file and pdf file
here is my code..

C#
var myMemoryStream = new MemoryStream();
Response.Clear();
 Response.ContentType = "Application/msword";
 Response.AddHeader("Content-Disposition";"attachment;filename=" + strFilename);
 Response.BinaryWrite(myMemoryStream.ToArray());
 Response.Flush();
           
 Response.Close();
 Response.End();
Posted
Updated 21-Feb-14 20:04pm
v2

Well, no - it won't.
Look at your code:
C#
var myMemoryStream = new MemoryStream();
Response.Clear();
 Response.ContentType = "Application/msword";
 Response.AddHeader("Content-Disposition";"attachment;filename=" + strFilename);
 Response.BinaryWrite(myMemoryStream.ToArray());

myMemoryStream is new, so contains no data - so the system (correctly) transfers no bytes...and that means no download.

Perhaps you forgot to load the MemoryStream with data from a file or database somewhere?
 
Share this answer
 
Comments
utm 22-Feb-14 15:22pm    
thanks for reply..
i have modified the code but still not works..
here is my code.


string filename = Server.MapPath("~/File/DocFile/) + strFilename;
byte[] file = File.ReadAllBytes(filename);
var myMemoryStream = new MemoryStream(file);
Response.ContentType = "Application/msword";
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
Response.BinaryWrite(myMemoryStream.ToArray());
Response.Close();
Response.End();



please if possible give me solution with code.. Smile | :)
OriginalGriff 22-Feb-14 15:37pm    
This may sound like a silly question, but did you look at your code?
You read a file into an array of bytes, in order to convert it to a stream, and convert that back to an array of bytes in order to send it to the client... Why?

And then, you close the HttpResponse... which throws everything away:
"This method terminates the connection to the client in an abrupt manner and is not intended for normal HTTP request processing. The method sends a reset packet to the client, which can cause response data that is buffered on the server, the client, or somewhere in between to be dropped."
MSDN: http://msdn.microsoft.com/en-us/library/system.web.httpresponse.close(v=vs.110).aspx

So, read the file, send the bytes directly to the client, and don't close the response!
string filename = Server.MapPath("~/File/DocFile/") + strFilename;
Response.Redirect("~/File/DocFile/" + strFilename);


these two lines r enough to download a file from your location.

thanks to
OriginalGriff gives some light on this topic.
 
Share this answer
 

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