Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question How to save files from WebGL app to hosting server?

Discussion in 'Web' started by JonasDietrich, May 23, 2024.

  1. JonasDietrich

    JonasDietrich

    Joined:
    May 23, 2024
    Posts:
    2
    Hello community,

    For a small study I designed an Unity apps that each experimentee has to play through. For easy access I want the game to be available online (no installation required) and thus I chose the WebGL build. While playing the game some game data is logged and should be saved in a text file at the end.

    Currently my app is hosted only in my local network on a laptop and accessing the WebGL game works fine, but saving the data does not, or at least I can't seem to find the files anywhere. The development build in unity runs without a single issue.

    The game stats are simply saved in a file with the common SystemIO methods:
    Code (CSharp):
    1. void SaveGameStats() {
    2.     string languageFolder = isSubjectEnglish ? "English" : "German";
    3.     string validityFolder = isValidSubject ? "Valid" : "Invalid";
    4.     string savePath = Path.Combine(Application.dataPath , "Game Stats", languageFolder, validityFolder);
    5.  
    6.     if(!Directory.Exists(savePath)) {
    7.         Directory.CreateDirectory(savePath);
    8.     }
    9.  
    10.     DateTime localDate = DateTime.Now;
    11.     var cultureGerman = new CultureInfo("de-DE");
    12.     string saveFileName = localDate.ToString(cultureGerman);
    13.     saveFileName = saveFileName.Replace(".", "_");
    14.     saveFileName = saveFileName.Replace(":", "_"); //Save file name in  the format DD_MM_YY HH_MM_SS.txt
    15.     saveFileName += ".txt";
    16.    
    17.     Debug.Log("File saved:" + Path.Combine(savePath, saveFileName));
    18.  
    19.     using StreamWriter gameStatsFile = new StreamWriter(Path.Combine(savePath, saveFileName));
    20.     {
    21.     gameStatsFile.Write(dataAmountCorrectLevels.ToString() + "\n\n");
    22.     gameStatsFile.Write(dataChoiceTrueOrFalse + "\n");
    23.     gameStatsFile.Write(dataChosenOption + "\n");
    24.     gameStatsFile.Write(dataUsedTime + "\n");
    25.     }
    26. }
    My question regarding WebGL now:

    1) In which directory are files stored under WebGL stored?

    2) Are the saved locally in the browser, on the clients side file system or on the server side file system on which the WebGL game is setup?

    3) In case they are stored not on the server side, what would be the best approach to transfer the file to the server/hosting laptop.

    Thanks in advance. In case you need more information, feel free to ask me.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    Because File I/O in the web browser is sandboxed. Web apps don't have access to the user's file system. It all goes into some "indexed db" container whose location is somewhere under AppData - where the browser stores its cache files, and there in some random folder, and you will not find the files you wrote but just the index db container file(s).

    You need to use WebRequest to send "files" (string or byte[]) content elsewhere. To do so, you need to have some receiving side, a server service. Or some cloud storage. I bet there are even free services that accept post web requests where you can then download those files if they aren't too big.

    Since this is a study project, you could use the hacky platform-specific solution. Look for "unity webgl open file dialog" and get one of these (platform-specific) scripts where you can pop up a file dialog and let the user save a file. However with that you can at best suggest the intended location, but the user is free to chose where to save that file and how to name it - in case that might be an issue.
     
  3. JonasDietrich

    JonasDietrich

    Joined:
    May 23, 2024
    Posts:
    2
    I see, thanks for the reply. It is probably for the better in terms of security that the files are saved sandboxed.

    I will do some research and try out it this solution and keep in mind your ither suggestions.

    Thanks.
     
    CodeSmile likes this.