none
c# how to make firefox download file and not open up the link in a tab ? RRS feed

  • Question

  • ok i have been making a windows form app that at the end loads to files 

    one file.osgjs

    two model_file.bin

    i need the file one to open witch it does fine but the model.bin file i want to auto download does it by default on chrome firefox opens it up in a tab like so 

    but in google chrome and other browsers it just auto downloads it and not sure how i can make firefox do the same i been looking up settings and all that but no luck the code i use is simple one line of code to get it to download on chrome 

    System.Diagnostics.Process.Start(url);

    and that works fine on other browsers but firefox it seems to load the file instead of downloading it automaticly is there a way to force firefox to auto download it instead of loading it in a tab

    thanks in advance elfenliedtopfan5

    Friday, September 27, 2019 4:20 PM

All replies

  • Why are you invoking Chrome to do this?  You can use the HttpRequest class to download the file yourself.  It's not that much harder than creating a process, and you eliminate the dependency on a huge, bloated browser.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, September 27, 2019 4:38 PM
  • Why are you invoking Chrome to do this?  You can use the HttpRequest class to download the file yourself.  It's not that much harder than creating a process, and you eliminate the dependency on a huge, bloated browser.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    the reason i do it this way is because the httprequest returns this 

    {"The remote server returned an error: (403) Forbidden."}

    this is the code i was using prior to this 

    public async Task GoToSitemodelbinAsync(string url)
            {
                System.IO.Directory.CreateDirectory(elfenliedprogsettings.Read("DOWNLOADPATH") + "/" + displayname);
               // System.Diagnostics.Process.Start(url);
                await Task.Delay(2000);
                // Sachiko_Res.internetdownloadpath = Sachiko_Res.elfenliedprogsettings.Read("INTERNETDOWNLOADPATH");
               // modelfilecopy();
    
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                webRequest.Method = "GET";
                webRequest.Timeout = 3000;
                webRequest.BeginGetResponse(new AsyncCallback(PlayResponeAsync), webRequest);
    
    
    
            }
    
    
    
            private static void PlayResponeAsync(IAsyncResult asyncResult)
            {
                long total = 0;
                long received = 0;
                HttpWebRequest webRequest = (HttpWebRequest)asyncResult.AsyncState;
    
                try
                {
                    using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncResult))
                    {
                        byte[] buffer = new byte[1024];
    
                        FileStream fileStream = File.OpenWrite("model_file.bin.gz");
                        using (Stream input = webResponse.GetResponseStream())
                        {
                            total = input.Length;
    
                            int size = input.Read(buffer, 0, buffer.Length);
                            while (size > 0)
                            {
                                fileStream.Write(buffer, 0, size);
                                received += size;
    
                                size = input.Read(buffer, 0, buffer.Length);
                            }
                        }
    
                        fileStream.Flush();
                        fileStream.Close();
                    }
                }
                catch (Exception ex)
                {
                }
            }



    Friday, September 27, 2019 6:20 PM
  • Does the site require logging in?  If I try to wget that file, I also get "access denied."  It may be you have to simulate a login and grab a cookie to pass back.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Sunday, September 29, 2019 2:40 AM
  • Hi elfenliedtopfan2, 

    Thank you for posting here.

    For error about ‘The remote server returned an error: (403) Forbidden’, you can refer to the following references:

    1. The remote server returned an error: (403) Forbidden
    2. HttpWebResponse return The remote server returned an error: (403) Forbidden

    Besides, if you have more questions about firefox, I suggest you post in firxfox forum. The C# forum is for C# related question, we discuss and ask the C# programming language, IDE, libraries, samples and tools here.

    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 30, 2019 7:32 AM
    Moderator