I’m trying to download a zip file from my public GitHub repository using UnityWebRequest. I am able to download the file when I run my game in the editor, but I get an “Unknown Error” when I try to download it in my WebGL build. This only happens with .zip and other archive files like .rar, as I am able to download text and image files in build without issues.
I have tried different hosting platforms, thinking the issue may be with GitHub, but I still get the same issue no matter where I try to download the zip file from. I have also tried other zip/rar files, thinking the issue may be with my zip file. No luck. Removing the .zip/.rar extension name from the file didn’t help either.
Any help or advice on how I can download a zip file in the build would be appreciated, thanks!
IEnumerator GetFileFromWeb()
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(link))
{
yield return webRequest.SendWebRequest();
if (webRequest.result != UnityWebRequest.Result.Success)
{
// This happens every time in build for some reason, but doesn't happen in the editor
Debug.LogError("Error while loading image zip file from web: " + webRequest.error);
}
else
{
Debug.Log("Got a file with the byte length of " + webRequest.downloadHandler.data.Length.ToString());
}
}
}
EDIT
Here is the solution so you don’t have to search through the entire thread:
However, the link does not matter. I tried this with many other links, and as long as it leads to a zip/rar/tar file, the Web Request will spit out an Unknown Error before it even attempts to download the file when running in build.
The file size does not matter either. It will freak out the moment it sees a zip/rar/tar file of any size.
This should be easy to replicate: make a script in your WebGL project with the same basic Unity Web Request code that I shared above and try downloading a zip file from any link in edit mode. You shouldn’t get any errors. But when you build and run your WebGL application and try to download the same zip file, it spits out that Unknown Error.
I have tested running it with some CORS proxy services but 90% of them didn’t work, and the ones that did gave me a “Forbidden” error in UnityWebRequest.
Setting up my own server with a CORS proxy machine is above my coding skill level, but I did find a solution that promises an easy-to-set-up CloudFlare CORS proxy (https://github.com/Zibri/cloudflare-cors-anywhere). I’ll see if I can get this one to work and I’ll update this post if I have any luck with it.
EDIT:
I have tried the linked CloudFlare solution above and it worked. I am able to download my zip file in both Unity Editor and in the WebGL build.
However, I did stumble on potential “legal” issues, if anyone finds out you are running a CORS proxy, so I’ll keep looking for alternative solutions. For example, hosting my zip file contents as separate files (without any zips) and sending a new web request to each of them individually.
Your solution did not work. I still get the same “403 Forbidden” error in Unity Editor and an “Unknown Error” in the WebGL build no matter what I do with the link if it has a public CORS proxy service link attached to it. I even tried it with a simple text file (which I can download just fine without CORS proxy services) and it gave me a “Protocol Error” with that service.
Like I mentioned above, I did get it to work with this solution without needing to do any fancy conversions with the link: https://github.com/Zibri/cloudflare-cors-anywhere but it requires you to pay yearly for your own domain and can come with legal complications (from how I understood it), so for now, I’m trying to solve this by downloading each file with an individual web request from my repository, but let me know if I misunderstood the solution that you linked above.
Yeah that was the code that I used! I tried it with multiple other download links and they were all blocked. This may very well be an issue with my country (or my ISP (or my router (or my device))).
My mass-download test where I try to download all my zip content files in bulk worked great… until it triggered DDOS protection at the service where I was hosting my content.
My next attempt at downloading mass data is going to be Unity Asset Bundles. From what I read, it’s like zip files that only Unity apps can read, and there is even a custom web request option for downloading them, so they should work.
For reference, I am trying to download ~200 small png images to use as game textures, and I am hosting them remotely so I can update them freely at any time and add new images if I wish to. I also want these images to have name data that is easy to work with and edit, so I can’t have them all in a single big texture. I’ll try the Asset Bundle solution and send an update if it works.
Asset bundle solution did not work. It runs into the same Unknown Error when running the WebGL build, which means it most likely runs into the same CORS issue.
However, I have found a workaround (saving all asset info as values, strings, and bytes into a JSON text document and sending that over the internet) and I want to make a detailed GitHub page about it to summarize everything from this thread because I see I’m not the first person who’s running into this problem. I’ll post a link to it here once I write down all the documentation for it.
Please read through the posts above. Asset bundle downloads give the same download error in WebGL builds because of CORS. External asset bundles work great for a non-WebGL game that is ran directly on your device. If you are running your application on a remote server, like in the case of a WebGL app, this solution will produce CORS errors. If you read through the solution that you posted, there is a reason why it doesn’t give “a web game” as an applicable example of external asset bundle use.
having not tried asset bundles, i confess, i thought they would be with the html/javascript files and therefore cors wouldnt apply… thanks for letting me know thats not the case