I have a problem where i have some issues to send canvas's imageData to my server to process it.
Here's my react/redux action to send data:
export const edit = (e) => dispatch => {
let payload = store.getState().image.imageData
payload = payload[payload.length-1]
console.log(payload)
const id = e.target.id ? e.target.id : e.target.parentElement.id
fetch(`/api/editing/${id}`, {
method: 'POST',
// headers: {
// "Content-Type": "application/json"
// },
body: JSON.stringify({ imgData: payload })
})
.then(res => res.json())
.then(json => {
dispatch({ type: UPDATE_IMAGE, payload: json.imgData })
// handle response display
})
}
Here's my how I handle request:
const traitement = require('../../processing/traitement')
router.post('/:type', (req, res) => {
const { imgData } = req.body
const method = req.params.type
const imgDataProcessed = traitement[method](imgData)
return res.status(200).json({ imgData: imgDataProcessed })
})
Here's an example of what a treatment method looks like:
negatif: function(imgData) {
console.log(imgData)
var n = imgData.data.length;
for(i=0; i<n; i+=4){
imgData.data[i] = 255 - imgData.data[i];
imgData.data[i+1] = 255 - imgData.data[i+1];
imgData.data[i+2] = 255 - imgData.data[i+2];
imgData.data[i+3] = 255;
}
return imgData;
},
the console.log() just before I send (what I expect to send):
ImageData {data: Uint8ClampedArray(180000), width: 300, height: 150}
data: Uint8ClampedArray(180000) [0, 0, 255, 255, 0, 0, 255, 255, 0, 0,
255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0,
0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0,
0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255,
255, …]
height: 150
width: 300
__proto__: ImageData
I can't paste what I get from the server but I lose the width, height keys, the typeof is Object instead of ImageData and the data key is typeof Object instead of Uint8ClampedArray.
So my question is: How can I do to make my route access the same data I send so I can be able to process it?
As you can see I send them as stringified json and I have a json bodyparser middleware on my server, maybe it's from there. I also though about the content-type header
EDIT: Thanks to Kaiido I've modified my code that way which seems to work at 1 exception
How I modified my code, front:
let payload = store.getState().image.imageData
payload = payload[payload.length-1]
const id = e.target.id ? e.target.id : e.target.parentElement.id;
console.log(payload)
const meta = {
width: payload.width,
height: payload.height
}
const formData = new FormData();
formData.append("meta", JSON.stringify(meta));
formData.append("data", new Blob([payload.data.buffer]))
fetch(`/api/editing/${id}`, {
method: "POST",
body: formData
})
.then(res => res.arrayBuffer())
.then(buffer => {
console.log(buffer)
const data = new Uint8ClampedArray(buffer)
console.log(data.length)
const newImg = new ImageData(data, payload.width, payload.height)
return newImg
})
back:
router.post('/:type', (req, res) => {
let form = new formidable.IncomingForm()
form.parse(req, (err, fields, files) => {
fs.readFile(files.data.path, (err, data) => {
const imgProcessed = traitement[req.params.type](data)
console.log(imgProcessed.length)
return res.status(200).json([imgProcessed])
})
})
})
1 issue left: assuming that for testing I'm using a 150300px image my data array should be 180000 long (300150*4) which it is until I send server's response.
When the front receives response it calls res.arrayBuffer()
then it creates a new Uint8ClampedArray but then my length is no more 180000 but 543810 in that case.
As Kaiido said I might want to slice that array, which I tried, but doesn't works.
How should I slice it? 180000 firsts ones? 180000 lasts ones? some other way?