I am making an image compressor. In the image you see a simple design with a dragon drop to fill in ur files. But i want to download the image i keep getting one error (displayed below).
[This is what i got so far.][1] [1]: https://i.stack.imgur.com/2RJ3v.png
This is my download funtion but when i press the button to download i keep getting 1 error
JavaScript
x
12
12
1
function download(file, res) {
2
console.log(file);
3
var fdata = new FormData()
4
fdata.append('upload_preset', 'image_name')
5
fdata.append('file', file)
6
// converts the picture and instant download the new image.
7
imageConversion.compressAccurately(file, 50).then(res=>{
8
console.log(res)
9
imageConversion.downloadFile(res)
10
})
11
}
12
Error:
conversion.js:1 Uncaught (in promise) Error: compressAccurately(): First arg must be a Blob object or a File object.
I tried a lot of things but i can’t really figure it out. Someone got any idea how to solve this?
Advertisement
Answer
I figured it out. I had to add an Array and make the file excisable for all functions.
JavaScript
1
16
16
1
function handleFiles(files) {
2
window.files = files;
3
files = [files];
4
files.forEach(previewFile);
5
}
6
7
function download() {
8
Array.from(files).forEach((element) => {
9
// converts the picture and instant download the new image.
10
imageConversion.compressAccurately(element, 50).then((res) => {
11
console.log(res);
12
imageConversion.downloadFile(res, 'test');
13
});
14
});
15
}
16