0

I post to server raw byte [] of texture but shows 5B in sql database and when this data is downloaded the file is empty. Can you please provide guidance?

I post to server the raw byte[] of a texture like so:

byte [] imgByte0 = uploadedTex.GetRawTextureData();//uploadTex is Texture2D
Debug.Log("Byte Len " + imgByte0.Length);//results in 722160
WWWForm form = new WWWForm();;
form.AddBinaryData("rgbImgBytes", imgByte0, "image/png");
UnityWebRequest www = UnityWebRequest.Post("https:address.php", form);
yield return www.SendWebRequest();
I post it to my php and insert into sql database BLOB (I'm aware there are other preferred alternatives to store images, imgs are/will be stufficiently small not the question here):

$thisRGBImg=$_FILES['rgbImgBytes'];
$stmt = $conn->prepare("INSERT INTO $imageTable `thisRGBImg` VALUES (?)");
$stmt->execute([$thisRGBImg];

Png image is 300Kb, however it shows as 5B held in sql database BLOB. I further confirm when I download these raw bytes to file (as per https://thoughtbot.com/blog/avoiding-out-of-memory-crashes-on-mobile#streams-to-the-rescue) and resulting file is empty:

using (UnityWebRequest myWebRequest = UnityWebRequest.Post(path, formData2))
{
  myWebRequest.downloadHandler = new ToFileDownloadHandler(new byte[64 * 1024], savePathWithFileName);
  yield return myWebRequest.SendWebRequest();
}
public ToFileDownloadHandler(byte[] buffer, string filepath) : base(buffer)
    {
        this.filepath = filepath;
        fileStream = new FileStream(filepath, FileMode.Create, FileAccess.Write);
    }
protected override bool ReceiveData(byte[] data, int dataLength)
    {
        if (data == null || data.Length < 1)
        {
            Debug.Log("ReceiveData - received a null/empty buffer");
            return false;
        }
        received += dataLength;
        Debug.Log("Data received " + dataLength + " total received " + received);
        //if (!canceled) fileStream.Write(data, 0, dataLength);//replaced  for bw below
        if (!canceled)
        {
            var bw = new BinaryWriter(File.Open("path", FileMode.OpenOrCreate));
            bw.Write(data);
            bw.Flush();
            bw.Close();
        }
        return true;
    }
Sergio Solorzano
  • 476
  • 9
  • 29
  • 1
    well, check the uploaded file size, check before you put it in the db start there – BugFinder Feb 01 '20 at 15:32
  • Hi @BugFinder I'm testing raw imgByte0.length before www.SendWebRequest(); and it's 722160 – Sergio Solorzano Feb 01 '20 at 15:38
  • 1
    OK, so check your webserver has it.. was my point – BugFinder Feb 01 '20 at 16:01
  • php query at upload print_r($_FILES); returns the correct data size, but type seems incorrect would you know? yet AddBinaryData doesn't have a field for type [rgbImgBytes] => Array ( [name] => png [type] => application/octet-stream [tmp_name] => /tmp/phpmnfsC9 [error] => 0 [size] => 722160 ) – Sergio Solorzano Feb 01 '20 at 16:16
  • 1
    ok, but the contents is in a file.. not in memory... are you sure that $_FILES['rgbImgBytes'] is populated? I dont think it is by default, its saved as a file on the OS – BugFinder Feb 01 '20 at 16:32
  • yeah, i'm getting zero file size, tried a few ways but this would seem more robust: $thisRGBImg=$_FILES['rgbImgBytes']; $myfilename="tempRGBImgBytes.dat"; $myfile = fopen($myfilename, "wb") or die("Unable to open file!"); fwrite($myfile, $thisRGBImg); fclose($myfile); echo " file size " . filesize($myfilename); – Sergio Solorzano Feb 01 '20 at 17:01
  • well yes, you need to read it back into memory . – BugFinder Feb 01 '20 at 17:03
  • sorry, it confirmed size is in memory with print_r, what do you mean by read back? – Sergio Solorzano Feb 01 '20 at 17:06
  • 1
    As I said normally the content of _FILES is details about the file on disk, not something currently in memory, so you would read in _FILES$[tmp_name] into memory – BugFinder Feb 01 '20 at 17:17

0 Answers0