Uploading and Downloading Files

Save Game Pro provides API for uploading and download files, so it allows you to upload saved images, data files and other kind of files.

The SaveGameWeb class at BayatGames.SaveGamePro.Networking namespace is the main API class for handling with uploading and downloading of files using UploadFile and DownloadFile methods, so it means you can store files on your server storage instead of database and do other stuff with them, like displaying the content of the file to the user.

Here is a simple example for uploading and downloading files:

// The uploading process
IEnumerator DoUpload()
{
    SaveGameWeb web = new SaveGameWeb(url, secretKey, "test", "test");
    yield return web.UploadFile("helloWorld.txt", "helloWorld.txt");
    if (web.Request.isHttpError || web.Request.isNetworkError)
    {
        Debug.LogError("Upload Failed");
        Debug.LogError(web.Request.error);
        Debug.LogError(web.Request.downloadHandler.text);
    }
    else
    {
        Debug.Log("Upload Successful");
        Debug.Log("Response: " + web.Request.downloadHandler.text);
    }
}

// The downloading process
IEnumerator DoDownload()
{
    SaveGameWeb web = new SaveGameWeb(url, secretKey, "test", "test");
    yield return web.DownloadFile("helloWorld.txt", "helloWorld.txt");
    if (web.Request.isHttpError || web.Request.isNetworkError)
    {
        Debug.LogError("Download Failed");
        Debug.LogError(web.Request.error);
        Debug.LogError(web.Request.downloadHandler.text);
    }
    else
    {
        Debug.Log("Download Successful");
        Debug.Log("Response: " + web.Request.downloadHandler.text);
    }
}

And you can use JSON serialization format to make the stored data human readable and handle with the data on your server scripts. Check out Changing Serialization Method article

Now, you can access the uploaded file at the specified uploads folder at installation of Save Game Pro, the default path is the /uploads and the saved file path will be /uploads/test/helloWorld.txt