Reputation: 4467
I want to allow client to download attachment. Here attachment.AttachmentContent
is bytes array from file. When I click on the button to download attachment it runs this code below. But I have the following script error. What should I do to fix it ?
Uncaught Sys.WebForms.PageRequestManagerParserErrorException: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled. Details: Error parsing near '����JFIF``'.
Attachment attachment = _attachmentService.GetAttachmentBytesById(int.Parse(e.CommandArgument.ToString()));
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "image/jpeg";
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + "test.jpg" + "\"");
Response.OutputStream.Write(attachment.AttachmentContent, 0, attachment.AttachmentContent.Length);
Response.End();
Upvotes: 1
Views: 2429
Reputation: 17724
Seems you are sending this file download in partial response.
The most common cause if that your download button is inside an UpdatePanel.
Files can only be send on a full PostBack.
You can set your download button as a PostBack trigger to solve this.
<Triggers>
<asp:PostBackTrigger ControlID="Download_Click">
</Triggers>
Upvotes: 6