Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Response.BinaryWrite calls the ASPX page twice

Tags:

c#

asp.net

I am trying to open a file in the browser using BinaryWriter. This causes the a dialog window to open and prompt the user to save or open the file. That behavior is fine; however, if I select open, the aspx page is called again and after a long wait the file finally opens.

I set the ContentType

Response.BinaryWrite(binary);
Response.End();
Repsonse.Close();

This behavior only occurs with excel and word files.

Browser IE8

like image 229
Jon.ee Avatar asked Mar 24 '26 13:03

Jon.ee


1 Answers

A substitute way of doing the same. You can check it out:

FileInfo fileInfo = new FileInfo(yourFilePath);    
context.Response.Clear();   
context.Response.ContentType = contentType;   //Set the contentType
context.Response.AddHeader("content-disposition", "attachment; filename=" + Path.GetFilename(yourFilePath));   
context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());       
context.Response.TransmitFile(yourFilePath); 

Hope this will help

like image 88
Tapas Mahata Avatar answered Mar 26 '26 02:03

Tapas Mahata



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!