Why are .docx files being corrupted when downloading from an ASP.NET page?

前端 未结 8 942
长发绾君心
长发绾君心 2020-12-03 05:09

I have this following code for bringing page attachments to the user:

private void GetFile(string package, string filename)
{
    var stream = new MemoryStre         


        
8条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 05:12

    Take a look a this: Writing MemoryStream to Response Object

    I had the same problem and the only solution that worked for me was:

        Response.Clear();
        Response.ContentType = "Application/msword";
        Response.AddHeader("Content-Disposition", "attachment; filename=myfile.docx");
        Response.BinaryWrite(myMemoryStream.ToArray());
        // myMemoryStream.WriteTo(Response.OutputStream); //works too
        Response.Flush();
        Response.Close();
        Response.End();
    

提交回复
热议问题