I'm trying to PUT svg, a string and a png together to a Django REST api from a pure javascript client.
I'm currently stuck with error 400 "Upload a valid image. The file you uploaded was either not an image or a corrupted image." response from the backend.
The png is created with the Pablo-svg-library, which converts the svg to png (With "toImage()").
The file itself doesn't appear to be corrupt, it can be loaded with python pillow (Which is also used by Django's ImageField).
var file = new Blob([svgData], { type: 'text/xml'});
var img = Pablo(document.getElementById('stage')).toImage('png', appendImgToFormAndSend);
function appendImgToFormAndSend() {
var picture = new Blob([img[0].src], { type: 'image/png'});
var xhr = new XMLHttpRequest();
xhr.open("PUT", "http://myapi.local/upload_all_the_data");
var formData = new FormData();
formData.append('picture', picture, 'picture1.png')
formData.append('rawdata', file, 'rawdata');
formData.append('url', location.href);
xhr.send(formData)
};
xhr.onload = function() {
console.log("ANSWER");
console.log(this.responseText);
var data = JSON.parse(this.responseText);
console.log(data)
}
Have I got a conceptual misunderstanding here?
Uploading the identical file through Django's admin template works fine (Although the raw-data of the png in the request looks quite different), so I assume the error-response above is misleading and the issue is with the request/encoding/filetype.
The payload from the js-code above looks like this:
-----------------------------1331527982888194116440756954
Content-Disposition: form-data; name="picture"; filename="picture1.png"
Content-Type: image/png
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlgAAAGQCAYAA [snip]
-----------------------------1331527982888194116440756954
Content-Disposition: form-data; name="rawdata"; filename="rawdata"
Content-Type: text/xml
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 600 400">
<text x="193.17" y="104.88" font-size="30px" style="stroke: black; fill: rgb(111, 126, 14);">Hello World</text>
<rect x="128.24" y="135.89" width="89.22" height="14.74" style="stroke: black; fill: rgb(69, 214, 137);"></rect>
<circle cx="287.04" cy="166.81" r="44.18" style="stroke: black; fill: rgb(39, 203, 202);"></circle>
<circle cx="69.34" cy="41.28" r="56.59" style="stroke: black; fill: rgb(140, 165, 11);"></circle>
<text x="480.22" y="21.02" font-size="30px" style="stroke: black; fill: rgb(127, 87, 191);">Hello World</text>
</svg>
-----------------------------1331527982888194116440756954
Content-Disposition: form-data; name="url"
http://myurl:8080/svg_editor/index.html?pk=5978bc3a-a275-4114-94a5-f90063ba3641
-----------------------------1331527982888194116440756954--
The request's payload from Django's admin template in contrast is encoded.