Getting Error message When i try to write bytes to word document and try to open it....

Getting Error message When i try to write bytes to word document and try to open it....then i get error message saying that the file "Name og the file" cannot be opend because it is a problom with the content og the file.....And when i clik the Ok button on the error message i get another error or warning saying that: Found unreadable content in "filename". Do you want to re create content in the document.....

Can any one see what i have done wrong?
private void ReturnDoc(byte[] doc, string filename)
        {
            HttpResponse resp = HttpContext.Current.Response;
            resp.ClearContent();
            resp.ClearHeaders();
            resp.AddHeader("Content-Disposition", "attachment; filename=" + filename + ".docx");
            resp.ContentEncoding = System.Text.Encoding.UTF8;

           // resp.OutputStream.Write(doc, 0, (int)doc.Length);
            //resp.Write(doc);
            //resp.Flush();
            //resp.Close();
            
            //*****************
            resp.Buffer = true;
            resp.BufferOutput = true;
            resp.AddHeader("Content-Length", doc.Length.ToString());
            resp.BinaryWrite(doc);
            resp.Flush();
            resp.Close();


            HttpContext.Current.ApplicationInstance.CompleteRequest();

        }

Open in new window

AdnanAsked:
Who is Participating?
 
Kiran SonawaneProject LeadCommented:
Try this

Replace resp.AddHeader("Content-Disposition", "attachment; filename=" + filename + ".docx");

with

resp.AddHeader("Content-Length", new System.IO.FileInfo(" + filename  +".docx").Length);
0
 
AdnanAuthor Commented:
hmm something is wrong in your code line...???!
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Kiran SonawaneProject LeadCommented:
Did you get any error?
0
 
AdnanAuthor Commented:
THE BEST OVERLOAD METHOD FOR MATCH FOR'sYSTEM.WEB.HTTPRESPONSE.ADDHEADER(STRING,STRING)' HAS SOME INVALID ARGUMENTS WHEN I PASTE THE LINE IN MY CODE....
0
 
AdnanAuthor Commented:
I am facing a strange problem in IE, when I use this type code for transmit the file (PDF,Excel,Word-doc etc.).

When I export a file In IE as an attachment, it asks me OPEN or SAVE. When I click SAVE, it saves but when I click OPEN, the same dialogue appears again and ask me for retry to open...?!! Whats happening here?
0
 
AdnanAuthor Commented:
And iam not facing this problom in FireFox...every thing works fine i firefox, i get the file open doc dialog box and i click OK and document opens....but when iam using IE it wont work for some reason.
0
 
AdnanAuthor Commented:
i think the fault is here...

when i use this line of code :  resp.AddHeader("Content-Disposition", "attachment; filename=\"Saksliste.docx\"");

 it wont work as i want it to, the browser aske me for open the file "FileName.docx" and when i click on open button it gave me another option to retry and when i click on retry it open the word but gives an error message saying that the file is corrupt or broken.....

and if i change the file name to only filename=\"Saksliste\", then it gives me option to save tyhe document first and then option to click on Open, when i click open it give a open with Dialog box.....

How can i just open the document in word without getting any option to open with.....
0
 
AdnanAuthor Commented:
I found the solution....follow the link and read...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.