Answer the question
In order to leave comments, you need to log in
What is the correct way to send the contents of the Canvas to the server?
There is a task - to implement the creation of a combined image (background, several inscriptions with shadows) and send it to VK on the community wall along with the text. This condition of the order is to make such an editor.
Now I also want it to work in Opera 11-12 (the appearance of shadows in different browsers is significantly different, at least in Opera and Chrome for sure, and it’s more convenient to calibrate for Opera, since I work in it all the time). But in Opera 11 there is no XHR2 and FormData, respectively, sending files natively does not work.
At the moment I have implemented the creation of the image. I also implemented for test purposes the export of the image to a Base64 string by the toDataURL() function and sending it to my own server as a regular form field (x-www-form-urlencoded) with subsequent decoding. Everything is great at this stage.
Next, I tried to make a POST request using the multipart / formdata method, read the standard, found examples, and sort of did it. At first, there was a problem that it was impossible to calculate the Content-Length in any way - for some reason the correct value did not fit, but then it turned out that if you did not specify it at all, the request went through normally.
But now I'm stuck at a very strange moment. The file is sent, PHP sees it. I have a string obtained from toDataURL() by trimming the beginning, and if I send it to the server and decode it from Base64 on the PHP side, everything is fine, the picture is correct. At the same time, I have two implementations of the decoding function from Base64, written in JS - I wrote one myself a long time ago, the second is taken from the network. Both give the same result (at least in terms of length). And this length matches the file size received after conversion on the server. But! As soon as I send the decoded string (with the usual xhr.send) - something completely different comes to the server. Moreover, when sending by different browsers, the size is different. But always in the resulting file some kind of nonsense is obtained.
56f8cc7d7d45f.png 976795 2016.03.28 09:17
56f8ccfa326dd.png 650807 2016.03.28 09:19
56f8ced2903ec.png 762761 2016.03.28 09:27
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question