I am using the following code to stream pptx which is in a MemoryStream object but when I open it I get Repair message in PowerPoint, what is the correct way of writing MemoryStream to Response Object?
HttpResponse response = HttpContext.Current.Response; response.Clear(); response.AppendHeader("Content-Type", "application/vnd.openxmlformats-officedocument.presentationml.presentation"); response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.pptx;", getLegalFileName(CurrentPresentation.Presentation_NM))); response.BinaryWrite(masterPresentation.ToArray()); response.End();
I had the same problem and the only solution that worked 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();
Assuming you can get a Stream, FileStream or MemoryStream for instance, you can do this:
Stream file = [Some Code that Gets you a stream]; var filename = [The name of the file you want to user to download/see]; if (file != null && file.CanRead) { context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); context.Response.ContentType = "application/octet-stream"; context.Response.ClearContent(); file.CopyTo(context.Response.OutputStream); }
Thats a copy and paste from some of my working code, so the content type might not be what youre looking for, but writing the stream to the response is the trick on the last line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With