views:

22

answers:

3

I have word document which is opening perfectly at server but when i download it using button click event of my website it gets currept. i am using below code on button click to make document download. please help to resolve this problem: i am using .net framework 3.5

 Response.Clear();
 Response.AddHeader("Content-Disposition", "attachment; filename=StandardLetter" + _responseId.ToString() + ".doc");
 Response.ContentType = "application/octet-stream";

 Response.TransmitFile (Server.MapPath("~/document/letter/StandardLetter" + _responseId.ToString() + ".doc"));
+1  A: 

Instead try:

Response.ContentType ="application/msword";

I dont use Word but for Excel I use:

Response.ContentType = "application/x-msexcel"
Markive
http://www.asptutorial.info/sscript/ContentType.html - here's a list of all of them.
Markive
I have tried but both are not making any change.
Rajesh Rolen- DotNet Developer
+1  A: 

Do you have a Response.End() after that code you posted? If not, you will get extra "html" code from the aspx file added to the transmitted file - thus corrupting it.

Hans Kesting
+1  A: 

Ok well here is the code I use, it's vb but easily converted ;)

 Response.ContentType = "application/pdf"
        Dim byteArray As Byte() = File.ReadAllBytes(MergedFile)

        Response.AddHeader("Content-Disposition", "attachment;filename=""" & ShortFilename & """")
        Response.AddHeader("Content-Length", byteArray.Length)
        Response.BinaryWrite(byteArray)
        Response.Flush()
        Response.End()

This works for PDF and by changing .ContentType to Excel spits that out too.. So I assume this will take any MIME type. Good luck!

I take my pdf document called MergedFile and convert it to a byte(), I give it a 'ShortName' that can be entered by the user. Content-Length is very important..

Markive