How to send/receive image from Unity to Django
Hi :)
i want to take a picture in Unity and send to Django server, and receive for image file.
i tried a bunch of things but it's not working..
i don't know why the send array and receive array size are difference
void TakeSnapshot()
{
Texture2D snap = new Texture2D(frontCam.width, frontCam.height);
snap.SetPixels(frontCam.GetPixels());
snap.Apply();
_SavePath = pathForDocumentsFile("photo");
System.IO.File.WriteAllBytes(_SavePath + ".png", snap.EncodeToPNG());
bytes = snap.EncodeToPNG();
//bytes = snap.EncodeToJPG();
UnityEngine.Object.Destroy(snap);
path = _SavePath + ".png";
StartCoroutine(ServerThrows());
}
IEnumerator ServerThrows()
{
////JSON
//string imageAsJson = File.ReadAllText(path);
//byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(imageAsJson);
//test = System.Text.Encoding.UTF8.GetBytes(imageAsJson);
//UnityWebRequest www = new UnityWebRequest("http://127.0.0.1:8000/", "POST");
//www.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
//www.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
//www.chunkedTransfer = false;
//www.SetRequestHeader("Content-Type", "application/json");
//yield return www.SendWebRequest();
//if (www.isNetworkError || www.isHttpError)
//{
// Debug.Log(www.error);
//}
//else
//{
// GetResponse(www);
//}
//UnityWebRequest
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
formData.Add(new MultipartFormDataSection("photo", bytes, "byte[]"));
//UnityWebRequest www = UnityWebRequest.Post(url, null, bytes);
UnityWebRequest www = UnityWebRequest.Post(url, formData);
www.chunkedTransfer = false;
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Form upload complete!" + www.downloadHandler.text);
}
}
def post(self, request, format=None):
print('get the data')
print('request.POST: ', request.POST)
print('request.headers: ', request.headers)
data_test = request.POST.get('photo', '')
# data_test = json.loads(request.body.decode('utf-8'))
print('data test is : ', data_test)
print('type of data_test is : ', type(data_test))
print('length of data_test is : ', len(data_test))
print('finish to get ')
# data_test = data_test.decode('utf-8').encode('euc_kr','replace')
f = open('./test.png','wb')
f.write(data_test.encode())
f.close()
# # # f = open('./test.jpg', 'w', encoding='UTF-8')
# f = open('./test.jpg', 'wb')
# # data_bytearray = bytearray(data_test)
# f.write(data_test)
# f.close()
# fh = open("./imageToSave.png", "wb")
# fh.write(data_test.decode('base64'))
# fh.close()
#
data = open('./test.png', 'rb')
# # data_in = data.read().decode()
# # data.close()
return HttpResponse("post !")
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612201808im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
POST request to RESTful APIs 0 Answers
WebRequest in unity 1 Answer
Why does UnityWebRequest not work for me? 0 Answers
Upload image from IOS gallery show no search result but works find in Android 0 Answers