-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
36 lines (33 loc) · 1.07 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
function removeBackground() {
var fileInput = document.getElementById("file");
var file = fileInput.files[0];
var formData = new FormData();
formData.append("image_file", file);
fetch("https://api.remove.bg/v1.0/removebg", {
method: "POST",
headers: {
"X-Api-Key": "EUxiqEScPmMNJy86Zn8EUa5o"
// "X-Api-Key": "8UvDuz97hr15MGsPGKfkadNS"
},
body: formData
})
.then(response => response.blob())
.then(blob => {
var url = URL.createObjectURL(blob);
var resultDiv = document.getElementById("result");
var image = new Image();
image.src = url;
resultDiv.appendChild(image);
// Create download link
var link = document.createElement("a");
link.href = url;
link.download = "background_removed.png";
var span = document.createElement("span");
span.innerHTML = "Download <i class='fa fa-download'></i>";
link.appendChild(span);
resultDiv.appendChild(link);
})
.catch(error => {
console.error(error);
});
}