Reputation: 8624
I have a Web Method that creates a file and gives it to the browser.
In Chrome the text file just downloads.
In IE9 the browser asks me if I want to open or save the file.
What I want to do is for the browser to automatically open the file (in the default program for that file type)
Code:
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public void Test(string docNum, string docVersion)
{
var response = Context.Response;
response.ContentType = "application/octet-stream";
response.AppendHeader("Content-Disposition", "attachment; filename=" + docNum + ".txt");
Byte[] stream = System.Text.Encoding.ASCII.GetBytes("docNum + "," + docVersion);
response.OutputStream.Write(stream, 0, stream.Length);
response.Flush();
}
I envoke it via:
http://localhost:12345/Services/Foo.asmx/Test?docNum=123456789&docVersion=1
How can I get the browser to just open the file?
Upvotes: 1
Views: 4483
Reputation: 8624
You CAN actually do this if the client computer knows how to open the file extension. I have gotten it to work in IE9
Assume you have a file extension .xyz
Pre-Rec: Client computer should have the file extension .xyz
registered to open with what every program you want it to open with. How to here: http://windows.microsoft.com/en-us/windows-vista/change-which-programs-windows-uses-by-default
STEP 1: In IIS set up a new MIME-type with properties of File name extension = .xyz
and MIME type = `application/xyz" How to here: http://technet.microsoft.com/en-us/library/cc725608(v=ws.10).aspx
STEP 2: On the code set the MIME-type to the one you just created and change the Content-Disposition to "inline":
response.ContentType = "application/xyz";
"Content-Disposition", "inline;
(Working Example for file types of .xyz
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public void Test(string docNum, string docVersion)
{
var response = Context.Response;
response.ContentType = "application/xyz";
response.AppendHeader("Content-Disposition", "inline; filename=" + docNum + ".xyz");
Byte[] stream = System.Text.Encoding.ASCII.GetBytes(docNum + "," + docVersion);
response.OutputStream.Write(stream, 0, stream.Length);
response.Flush();
}
That worked for me in IE9.
I have tried it in Chrome and it DID NOT work, it automatically downloaded the file, although I have not played with any settings in Chrome. If anyone has a cross-browser solution I would be interested.
Upvotes: 1
Reputation: 10844
Is probably more related to the type of files that you browser is configured to open.
If the browser does not know how to open certain type of files it will prompt you to download it
Another thing is that you have specified octect-stream for a TXT file
Content-Type: application/octet-stream
Content-Disposition: attachment;filename=\"My Text File.txt\"
Which actually forces the Browser to download the content as mentioned here
What content type to force download of text response?
Upvotes: 2
Reputation: 6744
If you want the browser to open a jpg or pdf or xls, you need to specify the correct mime type in your "ContentType" header. For instance, right now you are outputting everything as "octet-stream", which is very generic. The browser is not able to guess which program will open such a generic type. However, if it was a xls, you could use a ContentType of "application/vnd.ms-excel".
Here is a SO answer that points to an extensive list of resources for looking up possible mime types: https://stackoverflow.com/a/7192429/283895
Upvotes: 1