views:

482

answers:

3

Hi

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

+1  A: 

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.

jsight
OK, How do I control which to set as attachment and which not?
Shimmy
Well, you said it was an asp.net app, so I assumed that you would set it by controlling the Response.Headers variable. Or is this really a question about static file hosting with IIS?
jsight
IDK what static file hosting is, what I need is downloading files (of any type) from the server and change the file name. call it whatever you feel like :)
Shimmy
+1  A: 

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.

rally25rs
The problem is that if the file is not opened in the browser, I want to change it's filename.
Shimmy
So I need to know before it's rendered if it's going to open
Shimmy
OK, your original post didn't mention needing to change the filename. In that case, I can see your dilemma. You would normally set the filename like: Response.AppendHeader("Content-Disposition","attachment; filename=whatever.jpg"); However, I think that may always cause the Save As dialog to show in the browser. I don't think there is any way to possibly know what the browser can and can't render (for example they would have to have MS Office installed to open .doc Word documents, or Powerpoint to open .ppt, etc). Easiest might be to have a list of common types on the server and handle there.
rally25rs
+1  A: 

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();
Muhammad Akhtar
How will I know if the MIME is browser-compatible or not, you understand that pdf files open in the browser (in some browsers), I don't care what the solution should be whether a js function or whatever, that if the file doesn't properly open in this specific-client browser or for instance if acrobat is not installed, it should automatically show a save-as dialog.
Shimmy
Normally, pdf,doc,.. files, show dialog box to user, user select himself wheather he want to open or save file.....
Muhammad Akhtar