admin管理员组文章数量:1290202
Info on the API can be found here. It does not give any details for using with Javascript, only with curl.
Have tried numerous different methods from old posts on here but this is the closest I have got so far.
function main() {
var ul = document.querySelector('.redactor_toolbar')
if(ul != null)
{
var new_li = document.createElement('li')
var new_a = document.createElement('a')
new_li.appendChild(new_a)
ul.appendChild(new_li)
new_a.addEventListener('click', function() {
var input = document.createElement('input');
input.type = 'file';
input.onchange = e => {
uploadImage(e.target.files[0])
}
input.click();
})
}
}
async function uploadImage(img)
{
var form = new FormData();
form.append('image', img)
var url = ''
const config = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Access-Control-Allow-Origin': '*',
'Connection': 'keep-alive',
'Content-Type': 'application/json',
},
body: form
}
const response = await fetch(url, config)
const json = await response.json()
console.log(response)
}
The JSON response:
Info on the API can be found here. It does not give any details for using with Javascript, only with curl.
Have tried numerous different methods from old posts on here but this is the closest I have got so far.
function main() {
var ul = document.querySelector('.redactor_toolbar')
if(ul != null)
{
var new_li = document.createElement('li')
var new_a = document.createElement('a')
new_li.appendChild(new_a)
ul.appendChild(new_li)
new_a.addEventListener('click', function() {
var input = document.createElement('input');
input.type = 'file';
input.onchange = e => {
uploadImage(e.target.files[0])
}
input.click();
})
}
}
async function uploadImage(img)
{
var form = new FormData();
form.append('image', img)
var url = 'https://api.imgbb./1/upload?key=8d5867a9512390fb5e5dc97839aa36f6'
const config = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Access-Control-Allow-Origin': '*',
'Connection': 'keep-alive',
'Content-Type': 'application/json',
},
body: form
}
const response = await fetch(url, config)
const json = await response.json()
console.log(response)
}
The JSON response:
Share Improve this question asked Mar 23, 2020 at 7:48 NOP da CALLNOP da CALL 9312 gold badges10 silver badges17 bronze badges 1- does this api have any usage limitations or hidden charges? – Sujith S Manjavana Commented May 19, 2022 at 7:33
1 Answer
Reset to default 8is the same problem for mi application.
Create
<input type="file" id="input_img" onchange="fileChange()" accept="image/*">
The code javascript
function fileChange(){
var file = document.getElementById('input_img');
var form = new FormData();
form.append("image", file.files[0])
var settings = {
"url": "https://api.imgbb./1/upload?key=8d5867a9512390fb5e5dc97839aa36f6",
"method": "POST",
"timeout": 0,
"processData": false,
"mimeType": "multipart/form-data",
"contentType": false,
"data": form
};
$.ajax(settings).done(function (response) {
console.log(response);
var jx = JSON.parse(response);
console.log(jx.data.url);
});
}
This work for me
本文标签: ajaxHow to upload an image to ImgBB API using Javascript in a firefox addonStack Overflow
版权声明:本文标题:ajax - How to upload an image to ImgBB API using Javascript in a firefox addon - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741493513a2381728.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论