-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
46 lines (39 loc) · 1.01 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
let imageURL;
function submitHandler() {
console.log("click");
const fileInput = document.getElementById("fileInput");
console.log(fileInput.files);
const image = fileInput.files[0];
// Multipart file
const formData = new FormData();
formData.append("image_file", image);
formData.append("size", "auto");
const apiKey = "Rrx68KPULNnVoE5YPXvYcvKx";
fetch("https://api.remove.bg/v1.0/removebg", {
method: "POST",
headers: {
"X-Api-Key": apiKey,
},
body: formData,
})
.then(function (reponse) {
return reponse.blob();
})
.then(function (blob) {
console.log(blob);
const url = URL.createObjectURL(blob);
imageURL = url;
const img = document.createElement("img");
img.src = url;
document.body.appendChild(img);
})
.catch();
}
function downloadFile() {
var a = document.createElement("a"); //<a></a>
a.href = imageURL;
a.download = "no-bg.png";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}