Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Can you download a file from a URL without the full file name?

Discussion in 'Scripting' started by Controser, May 8, 2021.

  1. Controser

    Controser

    Joined:
    Feb 3, 2018
    Posts:
    9
    So as you can see from my partial code below I am trying to download a file from VRChat's website. Everything works if I use a full file name however I am running into an issue. The team who uploads their files online change the file name every time they update it. For instance, the file name currently looks like this: "VRCSDK2-2021.04.21.11.58_Public.unitypackage". If they update their file then the date and time changes and my code breaks. Is there a way to write my file name so that it will always download the latest version and not break when they update? Such as a way to have the code look for any file starting with "VRCSDK2"?
    Thanks for all the help!


    Code (CSharp):
    1.         private static string sdkStart = "https://files.vrchat.cloud/sdk/";
    2.  
    3. ...
    4.  
    5.         private static void DownloadSDK(string assetName)
    6.         {
    7.             GetPath();
    8.             WebClient w = new WebClient();
    9.             w.Headers.Set(HttpRequestHeader.UserAgent, "Webkit Gecko wHTTPS (Keep Alive 55)");
    10.             w.QueryString.Add("assetName", assetName);
    11.             w.DownloadFileCompleted += FileDownloadCompleted;
    12.             w.DownloadProgressChanged += FileDownloadProgress;
    13.             string url = sdkStart + assetName;
    14.             w.DownloadFileAsync(new Uri(url), Path + assetName);
    15.         }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    They would have to provide a URL with that functionality. There's nothing you can do as a client to get this behavior. Often they will provide a -latest url or something.
     
  3. Controser

    Controser

    Joined:
    Feb 3, 2018
    Posts:
    9
    So another question then... if there is a link that redirects to the download of the SDK such as:
    https://vrchat.com/download/sdk2
    can you download the file from a redirect? I could not find a way to do that prior.
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,920
    This is working for me properly:
    Code (CSharp):
    1. private static void DoDownload()
    2. {
    3.     var w = new WebClient();
    4.     w.Headers.Set(HttpRequestHeader.UserAgent, "Webkit Gecko wHTTPS (Keep Alive 55)");
    5.     w.DownloadFileCompleted += FileDownloadCompleted;
    6.     w.DownloadProgressChanged += FileDownloadProgress;
    7.     var url = "https://vrchat.com/download/sdk2";
    8.     w.DownloadFileAsync(new Uri(url), Application.dataPath + "/assetName.unitypackage");
    9. }
    10. private static void FileDownloadProgress(object sender, DownloadProgressChangedEventArgs e) => Debug.Log($"Progress: {e.ProgressPercentage}%");
    11. private static void FileDownloadCompleted(object sender, AsyncCompletedEventArgs e) => Debug.Log("Done.");
    screenshot1.png
     
    Last edited: May 8, 2021
    Controser, PraetorBlue and Bunny83 like this.
  5. Controser

    Controser

    Joined:
    Feb 3, 2018
    Posts:
    9
    Thank you so much ;-; I had much such a minor oversight and your code has completely fixed my issue!