Reputation: 104771
I am writing a file using Response.WriteFile(path);
My problem is that it always shows the save as dialog, what I want is that if the file type is jpg, pdf, or any browser compatible file it should open it in the browser; the save dialog should only open for any other browser-incompatible file
Upvotes: 1
Views: 1354
Reputation: 52241
For Image your code should be look like this.......
Response.ContentType = dtbl[0].FileExt;
Response.BinaryWrite(dtbl[0].ResData);
for document your code should be look like this....
Byte[] bytes = (Byte[])dtbl.Rows[0]["ResData"];
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = dtbl.Rows[0]["FileExt"].ToString();
Response.AddHeader("content-disposition", "attachment;FileName=" + dtbl.Rows[0]["DocName"].ToString());
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
Upvotes: 1
Reputation: 28419
You can largely control this behavior with the Content-Disposition header.
In order to force the browser to show SaveAs dialog when clicking a hyperlink you have to include the following header in HTTP response of the file to be downloaded:
Content-Disposition: attachment; filename=<file name.ext>
Where is the filename you want to appear in SaveAs dialog (like finances.xls or mortgage.pdf) - without < and > symbols.
Upvotes: 2
Reputation: 43728
What you described there should be the default behavior. The browser will decide what it can and can't render based on the MIME / content type. In your ASP.NET code, set Response.ContentType to the appropriate MIME type, and use the Response's output stream to send the file contents back tot he browser.
Available MIME types for IE are discussed here.
Upvotes: 1