C
C
c4ptain_h3mp2020-01-23 12:16:13
ASP.NET
c4ptain_h3mp, 2020-01-23 12:16:13

How to solve the problem: Response does not return .docx for download to the user's browser?

Good afternoon. I am developing a method on ASP.NET-WebForms. The bottom line is this: the user presses a button, a piece of data is formed on the front, which is transferred to the handler on the back. Additional data is selected in the handler, a .docx template is opened, edited using OpenXML tools, and the contents of the resulting file are written to a MemoryStream, which must be returned to the user in Response. Here is the handler code without editing the document itself, declaring and initializing variables.

//Объявление и инициализация переменных
byte[] FileBytes = File.ReadAllBytes(Template);
using (MemoryStream stream = new MemoryStream(FileBytes))
{
    using (WordprocessingDocument myDoc = WordprocessingDocument.Open(stream, true))
    {
        MainDocumentPart mainPart = myDoc.MainDocumentPart;

        foreach (SdtElement obj in mainPart.Document.Body.Descendants<SdtElement>().ToList())
        {
            foreach (Text t in obj.Descendants<Text>().ToList())
            {
                switch (t.Text)
                {
                    //.... редактирование файла .docx
                }
            }
        }
        myDoc.MainDocumentPart.Document.Save();
        myDoc.Close();
    }

    context.Response.Clear();
    context.Response.ClearHeaders();
    context.Response.ClearContent();

    context.Response.AddHeader("content-disposition", "attachment; filename=\"" + DocName + ".docx\"");
    context.Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    context.Response.ContentEncoding = Encoding.GetEncoding("ISO-8859-1");

    stream.Seek(0, SeekOrigin.Begin);
    context.Response.BinaryWrite(stream.ToArray());
}

context.Response.Flush();

JS function code:
function save_word_doc(id_btn) {
// ... here is code that gets params from default page data

jQuery.post("DataHandler.ashx?CN=" + vCN + "&CommandName=SaveWord&auctionID=" + oFormRecord.auctionID
    + "&user_login=" + user_login
    + ....
    + "&amount=" + oFormRecord.value.amount
    + "&percent=" + percent);
}

This code was tested on the initial ASP.NET WebForms project , initially for the possibility of saving the generated file at least locally - it works, the file is saved to the local machine (server) without damage.
I also assembled this functionality on ASP.NET-MVC with the given C# handler code, which is called from the LinkButton, in which the handler with the code above is specified in the PostBackURL - everything works, the browser downloads the generated file to the user, as in the case above.
But on the initial project, the download of the file does not start. What could be the problem ? Thanks in advance to everyone who takes the time to answer.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question