- Home /
The question is answered, right answer was accepted
Can you download a file from a URL without the full file name?
So as you can see below I am downloading a file from VRChat's website. Everything works when I use the full name of the file. However, their filenames change every time they update the file. For instance, the file name currently looks like this:
"VRCSDK2-2021.04.21.11.58_Public.unitypackage".
If they update the file then the name change breaks my code. Is there a way to write my file name so that it will 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!
private static string sdkStart = "https://files.vrchat.cloud/sdk/";
...
private static void DownloadSDK(string assetName)
{
GetPath();
WebClient w = new WebClient();
w.Headers.Set(HttpRequestHeader.UserAgent, "Webkit Gecko wHTTPS (Keep Alive 55)");
w.QueryString.Add("assetName", assetName);
w.DownloadFileCompleted += FileDownloadCompleted;
w.DownloadProgressChanged += FileDownloadProgress;
string url = sdkStart + assetName;
w.DownloadFileAsync(new Uri(url), Path + assetName);
}
Answer by Controser · May 08, 2021 at 06:35 PM
Never mind! I found the solution with someone else's help. For those who may need to know, if you have a link that redirects to the download then you can do this:
private static void DoDownload()
{
var w = new WebClient();
w.Headers.Set(HttpRequestHeader.UserAgent, "Webkit Gecko wHTTPS (Keep Alive 55)");
w.DownloadFileCompleted += FileDownloadCompleted;
w.DownloadProgressChanged += FileDownloadProgress;
var url = "link here that is the redirect";
w.DownloadFileAsync(new Uri(url), Application.dataPath + "/assetName.unitypackage");
}
private static void FileDownloadProgress(object sender, DownloadProgressChangedEventArgs e) => Debug.Log($"Progress: {e.ProgressPercentage}%");
private static void FileDownloadCompleted(object sender, AsyncCompletedEventArgs e) => Debug.Log("Done.");