1

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
kralco626
  • 8,456
  • 38
  • 112
  • 169
  • 2
    You can't force open an external program. It would be a huge security risk if you could. – JJJ Aug 29 '13 at 14:59
  • @Juhana - Agreed; However I'm not trying to force open an external program. I'm trying to get the browser to open the file automatically. There are definitely links that we have that the browser opens the file automatically rather than asking the user to choose open or save. – kralco626 Aug 29 '13 at 15:01
  • Yes, and you can't do that. It's completely up to the browser and the user which programs open automatically. – JJJ Aug 29 '13 at 15:03
  • We have a legacy cold fusion service that does the same thing, and IE does NOT ask the user if the file should be opened or saved. It just opens it. When I use this service, IE asks me. – kralco626 Aug 29 '13 at 15:04
  • 1
    Can you be a little more specific about the file type? I see that you are outputting the file as "octet-stream". That is a VERY generic type. If you want the browser to open a jpg or pdf or xls, you would be better-off using a more specific mime type. So, what type(s) are you outputting? – tgolisch Aug 29 '13 at 15:06
  • Check what headers the CF service sends when it serves the file and copy them. (The point stands; unless the browser is configured to open the file it won't do it.) It's also possible that the CF site is set to less restrictive zone in IE settings. – JJJ Aug 29 '13 at 15:07
  • The CF and this service are running in the same site - but I will check the headers – kralco626 Aug 29 '13 at 15:13
  • @tgolisch - It's actually a proprietary file type. However, if I select "open" in IE9 when prompted the file opens as expected. So maybe I can change a setting on the browser to tell IE to try to open this specific file type automatically? – kralco626 Aug 29 '13 at 15:15
  • @All - Thanks everyone for all your help, Yours guys ideas definitely helped me come to my solution. I posted it below in case anyone is interested. – kralco626 Aug 29 '13 at 15:49
  • ASMX is a legacy technology, and should not be used for new development. WCF should be used for all new development of web service clients and servers. One hint: Microsoft has retired the [ASMX Forum](http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/threads) on MSDN. – John Saunders Aug 29 '13 at 16:21
  • Yes, I know. However we have existing asmx services and were not going to start using WCF at this moment just because Microsoft wants us to. However, we will at some point switch over when we have the time to do the transition. – kralco626 Aug 29 '13 at 16:32

3 Answers3

2

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?

Community
  • 1
  • 1
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
  • File types are the same. We have a legacy ColdFusion application that does this same thing and the file is automatically downloaded. – kralco626 Aug 29 '13 at 15:07
  • 1
    Actually on second thought it is a different file type. The CF program is getting the actual document, where this web services is returning a proprietary file type that opens with a proprietary program and gets the actual document, so this could be it. – kralco626 Aug 29 '13 at 15:17
1

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

Community
  • 1
  • 1
tgolisch
  • 6,549
  • 3
  • 24
  • 42
  • if I know the application name, but it is not a standard mime type is there a way for me to tell the browser to try opening it with that application? – kralco626 Aug 29 '13 at 15:18
  • The file extension is registered with the OS, so it does know how to open it. However, there is no Mime type for this file type that I know of. Am I stuck? Is there any modification I can make to my server code or to the browser settings to make it try to open this file type automatically? – kralco626 Aug 29 '13 at 15:26
  • Since you registered your file extension with your OS, you are soooo close. You also need to create a ContentType for that extension. I usually do that via a quick registry hack. Example: Use RegEdit to take a look at "HKEY_CLASSES_ROOT\.pdf". You can see there is a key called "Content Type" with a value "application/pdf". That is the ContentType for .pdf. You could make up your own like "application/kralco626" and set that as the ContentType. You would have to do that on each machine (or have your exe set that). Then you can output that ContentType from your web server. – tgolisch Aug 29 '13 at 15:58
  • (safety message) normally, people are advised to stay out of the registry and I would feel weird suggesting it. However, since you have already registered the file extension with your OS, it means you already did 90% of the registry tweaking. You just missed this one last thing. – tgolisch Aug 29 '13 at 16:02
1

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.

kralco626
  • 8,456
  • 38
  • 112
  • 169