Save Word Document to HttpResponse as Attachment for Download via Client Web Browser C# .NET | Save on Disk | Response.AddHeader ContentDisposition text/html

Hi,

I am using Aspose Words on a project currently, and am utilizing the Save overload that accepts a HttpResponse object and outputs the document to the response for the client to save or download. One of the parameters is the FileName, as expected, and I have found that I cannot pass a filename that includes a Period and spaces in it that where the period does not denote the file extension. The returned name from the Save method (save/open file dialog in browser) shows the name ending at the first period, as if the rest of the filename were the extension.

To sample this, simply load any document and call

wordDoc.Save(HttpContext.Current.Response, "This.is a test.docx", ContentDisposition.Attachment, null);

The Save/Open dialog in the browser will display “This.is” as the filename.

If you then change the filename to “This.is+a+test.docx” the Save/Open dialog in the browser will display “This.is+a+test.docx” as the filename, as you expect.

Has this been encountered by anyone else, and if so, was there a workaround utilized for it? Currently, my only option to preserve most of the filename is to use Uri.EscapeDataString to encode the filename.

Thanks.

Hi Daved,

Thanks for your inquiry. While using the latest version of Aspose.Words for .NET i.e. 14.3.0, I was unable to reproduce this issue on my side. I would suggest you please upgrade to the latest version of Aspose.Words. You can download it from the following link:
https://releases.aspose.com/words/net

I hope, this helps.

Best regards,

Thanks for the follow-up.

If you refer to the attached screenshot, you will see that the DLL is showing a version 14.3.0 so I believe I am on the version you are recommending.

I simplified a test on a page load and the sample code is below. An additional screenshot is added to show the resulting Save dialog for this file when I use the code below:

protected void Page_Load(object sender, EventArgs e)
{
    Document testDoc = new Aspose.Words.Document(@"C:\TestDoc3.docx");
    testDoc.Save(HttpContext.Current.Response, "Mr. Anderson Document.docx",
    Aspose.Words.ContentDisposition.Attachment,
    null);
}

This has been consistent with producing that result, with any file name, given there is a period that exists in the name prior to the extension.

Thanks again.

Hi Daved,

Thanks for the additional information. There is no problem with Aspose.Words API. This issue is specific to FireFox web browser. Please use the following code to workaround this problem:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertHtml("<b>This is some content</b>");
MemoryStream stream = new MemoryStream();
doc.Save(stream, SaveFormat.Docx);
Response.Clear();
// Specify the document type.
Response.ContentType = "application/word";
// Other options:
// Response.ContentType = "text/plain"
// Response.ContentType = "text/html"
// Specify how the document is sent to the browser.
// Response.AddHeader("content-disposition","attachment; filename=MyDocument.doc");
// Another option could be:
Response.AddHeader("content-disposition", "inline; filename=\"This.is a test.docx\"");
// Get data bytes from the stream and send it to the response.
byte[] bytes = stream.ToArray();
Response.BinaryWrite(bytes);
Response.End();

I hope, this helps.

Best regards,