none
C# File download does not start when invoke or execute a url RRS feed

  • Question

  • if anyone load this url https://de.visiblealpha.com/links/80488d55-ae41-4def-9452-bae3ac2e2b06 into browser then a excel file start download. so when i invoke the same url by HttpWebRequest then excel file does not start download. this code example i tried.
        string address = "https://de.visiblealpha.com/links/80488d55-ae41-4def-9452-bae3ac2e2b06";
        using (WebClient client = new WebClient())
        {
            client.DownloadString(address);
        }
    again i tried this one too.
        string url = "https://de.visiblealpha.com/links/80488d55-ae41-4def-9452-bae3ac2e2b06";
        WebRequest request = HttpWebRequest.Create(url);
        WebResponse response = request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string responseText = reader.ReadToEnd();
    but failed to reach my goal. code successfully executed but no excel file start downloading which i am trying to achieve.

    when i tried to load this url https://de.visiblealpha.com/links/80488d55-ae41-4def-9452-bae3ac2e2b06 into webbrowser control then also saw same problem no excel file start download. here is code which i tried.
     webBrowser1.Navigate("https://de.visiblealpha.com/links/80488d55-ae41-4def-9452-bae3ac2e2b06");
    webBrowser1.ScriptErrorsSuppressed = true;
    i just do not understand why excel file is not getting download when invoke or execute the same very url.

    so please some one tell me what i need to do as a result the moment i will execute the url excel file will start downloading in client pc.

    please share some working code example. thanks
    • Edited by Sudip_inn Wednesday, December 12, 2018 9:07 PM
    Wednesday, December 12, 2018 1:43 PM

All replies

  • You are wrong when you say that the file is not downloaded. It is downloaded, but you are just throwing away the result of the download and not doing anything with it.

    In the first example, when you do

    client.DownloadString(address);

    you should actually be doing

    somevariable = client.DownloadString(address);

    The DownloadString method downloads the file into memory and then converts it to string, and the string is assigned to the variable. If you don't assign it to a variable, it is lost after downloading.

    Of course, given that the excel file is not a string, it will be completely corrupted when converted to a string and it will be useless. You will want to use a different method instead of DownloadString (for instance, DownloadFile) so that you can download the binary content.

    The same applies for your second example: When you call GetResponseStream you get a Stream that returns the binary content. You should not connect it to a StreamReader because this will convert the binary to string and the file will be corrupted. A reasonable alternative is to use Read on the Stream, which will pump the binary content of your Excel file into a byte array.

    In either case, at this point you have downloaded the file. You have the bytes of the file. This doesn't mean that the Excel application will open on screen and pick up those bytes. This is something that is done by the browser, but neither the WebClient nor the WebRequest provide this functionality. If you want it, you will need to code it yourself. You will have to take the bytes that you downloaded in the preceding steps, save them to a temporary file on disk, and then use the Process class to launch Excel and pass the temporary file as a parameter.

    Wednesday, December 12, 2018 10:08 PM
    Moderator
  • Hi Sudip_inn,

    If you want to download the file from the url, you could try the code.

                using (WebClient webClient = new WebClient())
                {
                    webClient.DownloadFile("https://de.visiblealpha.com/links/80488d55-ae41-4def-9452-bae3ac2e2b06/", "2018101800220728_MODEL_UPDATE.xlsx");
                }

    But, the url you provided is using a special format, I do not know that, so the file I download could not open. You need to check the content format of the url.

    Best Regards,

    Wendy


    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, December 17, 2018 8:01 AM
    Moderator
  • if you open  this link in browser 

    https://de.visiblealpha.com/links/80488d55-ae41-4def-9452-bae3ac2e2b06

    then you must notice when page load in browser then a excel file gets download at client side by javascript dynamically. so if i save this link https://de.visiblealpha.com/links/80488d55-ae41-4def-9452-bae3ac2e2b06 as excel then it will not serve my purpose.

    tell me any other work around. thanks

    Tuesday, December 18, 2018 2:45 PM