John Davidson

c# - Upload file to server(Unity)

0 comments
Message:


So I have looked at the unity document and some tutorials on youtube, I managed to create a localhost server and put some data on the server. Right now I can download the file that I have uploaded.


My problem is how to upload to the Webserver, when I upload the string I want to upload, the debug.log show success. However, when I opened the file on the server, the string I uploaded didn't write into the 123.txt file.


What I'm trying to do here is upload a string/JSON file to the server and download it, I am able to download it but not upload it. Some tutorials online show some PHP scripts, is it possible that I upload a string to the server and automatically turn it into a txt file and stored?


I try to set the path to http://localhost/Project/, which I want to do is to store the string or JSON file to the Project file. However, it results in Http404Notfound. If I do http://localhost/Project/123.txt, it is a success for upload. But the data I uploaded didn't get written into the 123txt file.


What should I do from here?


Upload_Code


IEnumerator Upload()
{
WWWForm form = new WWWForm();
form.AddField("myField", "myData");
byte[] myData = System.Text.Encoding.UTF8.GetBytes("This is some test data");

using (UnityWebRequest www = UnityWebRequest.Put("http://localhost/Project/123.txt", myData))
{
yield return www.SendWebRequest();

if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log(www.error);
}
else
{
ServerResult.text = www.downloadHandler.text;
Debug.Log("Form upload complete!");
}
}
}

I'm able to download a picture that I manually drag and drop to the server.


Download_From_Server


private void GetTexture(string url, Action<string> OnError, Action<Texture2D> OnSuccess)

{
StartCoroutine(GetServerTexture(url,OnError,OnSuccess));
}
private IEnumerator GetServerTexture(string url, Action<string> OnError, Action<Texture2D> OnSuccess)
{
using (UnityWebRequest unityWebRequest = UnityWebRequestTexture.GetTexture(url))
{
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError)
{
OnError(unityWebRequest.error);
}
else
{
DownloadHandlerTexture downloadHandlerTexture = unityWebRequest.downloadHandler as DownloadHandlerTexture;
OnSuccess(downloadHandlerTexture.texture);
//Debug.Log("Received: " + unityWebRequest.downloadHandler.text);
//ServerResult.text = unityWebRequest.downloadHandler.text;

}
}

}


My Answer:
Hello! How can I assist you today?

Rate this post

3 of 5 based on 2364 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us