本文介紹了如何在 firefox 插件中使用 Javascript 將圖像上傳到 ImgBB API的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
可以在
解決方案
小米應用也是同樣的問題.
創(chuàng)建
代碼 javascript
函數fileChange(){var file = document.getElementById('input_img');var form = new FormData();form.append("圖片", file.files[0])變量設置 = {"url": "https://api.imgbb.com/1/upload?key=8d5867a9512390fb5e5dc97839aa36f6",方法":發(fā)布",超時":0,過程數據":假,"mimeType": "multipart/form-data",內容類型":假,數據":表格};$.ajax(settings).done(function (response) {控制臺日志(響應);var jx = JSON.parse(響應);控制臺.log(jx.data.url);});
}
這對我有用
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.com/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:
解決方案
is 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.com/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
這篇關于如何在 firefox 插件中使用 Javascript 將圖像上傳到 ImgBB API的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!