• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by justinenayat · Oct 02, 2021 at 09:53 PM · serverdownloadwebrequestuploadinternet

Upload/download small files from internet in game?

I need the player to be able to upload and download small files to and from their application's persistent data path. Here is the code I have right now:

     IEnumerator UploadFileData()
     {
         string s_playerID = PlayFabManager.loggedInPlayfabId;
         string s_levelID = PlayFabManager.levelID;
 
         using (var uwr = new UnityWebRequest("https://example.com/", UnityWebRequest.kHttpVerbPUT))
         {
             string path = Path.Combine(Application.persistentDataPath, $"{s_playerID}{s_levelID}.replay");
             uwr.uploadHandler = new UploadHandlerFile(path);
             yield return uwr.SendWebRequest();
             if (uwr.result != UnityWebRequest.Result.Success)
                 Debug.LogError(uwr.error);
             else
             {
                 Debug.Log("File successfully uploaded.");
             }
         }
     }
 
     IEnumerator DownloadFile()
     {
         string l_playerID = PlayFabManager.load_PlayfabId;
         string l_levelID = PlayFabManager.load_levelID;
 
         var uwr = new UnityWebRequest("https://example.com/", UnityWebRequest.kHttpVerbGET);
         string path = Path.Combine(Application.persistentDataPath, $"{l_playerID}{l_levelID}.replay");
         uwr.downloadHandler = new DownloadHandlerFile(path);
         yield return uwr.SendWebRequest();
         if (uwr.result != UnityWebRequest.Result.Success)
             Debug.LogError(uwr.error);
         else
             Debug.Log("File successfully downloaded and saved to " + path);
     }

The code needs to ID which player made the file and what level the file was made on, if you were curious what the "s_playerID" stuff is. I understand I need to replace the "https://example.com/" with a URL to send and receive the data, but I don't know of a file hosting service to use and how to get the proper URL from it. It would be nice if I could just do it with dropbox or google drive, but I hear its not that simple. Are there any free file hosting services that would fit my needs here? Since the files are so small, I can make do with pretty tight storage limits. Any suggestions are super appreciated!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by rh_galaxy · Oct 03, 2021 at 08:08 AM

Most ISP does not support PUT, but you can do it with POST and the web database. Here is a full example, but it's cut from a bigger project so some variables aren't there, but you should get the idea. It also sends and stores all data as base64. You will need knowledge to make the webserver php-files.

 //probably unity is escaping the & and other chars so $_POST in php
 //contained only one big element with everything
 // luckily we can build our own raw sender:
 UnityWebRequest CreateUnityWebRequest(string url, string param)
 {
     UnityWebRequest requestU = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST);
     byte[] bytes = System.Text.Encoding.UTF8.GetBytes(param);
     UploadHandlerRaw uH = new UploadHandlerRaw(bytes);
     //uH.contentType = "application/json"; //this is ignored?
     requestU.uploadHandler = uH;
     requestU.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
     DownloadHandler dH = new DownloadHandlerBuffer();
     requestU.downloadHandler = dH;
     return requestU;
 }

 public IEnumerator SendHiscore(string szLevel, int iScoreMs, Replay oReplay)
 {
     bIsDone = false;
     byte[] bytes = oReplay.SaveToMem();
     string base64 = System.Convert.ToBase64String(bytes);

     string url = WEB_HOST + "/achievements_post.php";
     string data= "LEVEL="+ szLevel + "&NAME="+ UnityWebRequest.EscapeURL(GameManager.szUser) + "&USERID="+ UnityWebRequest.EscapeURL(GameManager.szUserID) + "&SCORE="+ iScoreMs + "&REPLAY="+ base64;

     www = CreateUnityWebRequest(url, data); //UnityWebRequest.Post(url, data); <- didn't work
     yield return www.SendWebRequest();

     if (www.isNetworkError || www.isHttpError)
     {
         Debug.Log(www.error);
     }
     else
     {
     }
     bIsDone = true;
 }

 public IEnumerator GetReplay(string szLevel, string szId, Replay oResult)
 {
     bIsDone = false;

     string url = WEB_HOST + "/hiscore_getreplay2.php?Level=" + szLevel + "&UserId=" + UnityWebRequest.EscapeURL(szId);
     www = UnityWebRequest.Get(url);
     yield return www.SendWebRequest();

     if (www.isNetworkError || www.isHttpError)
     {
         Debug.Log(www.error);
     }
     else
     {
         //retrieve results as text and convert it to binary
         byte[] bytes = System.Convert.FromBase64String(www.downloadHandler.text);

         oResult.LoadFromMem(bytes);
     }
     bIsDone = true;
 }


If you host your game (open source) on SourceForge they provide a web + database.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

128 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Como fazer download de arquivos enquanto o jogo funciona 0 Answers

Upload to FTP Server 1 Answer

Set up a server for downloading user built levels 4 Answers

Help with saving XML files with PHP to a server 0 Answers

protecting assetbundles on server 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges