Home > Mobile >  i want to upload images on imgbb with custom name
i want to upload images on imgbb with custom name

Time:11-18

i want to upload images on imgbb with API with custom name.
My current HTML was:

<input type="text" name="img_name" id="name" accept="text/*">
<input type="file" id="input_img" accept="image/*">
<input type="submit" value="Send" onclick="fileChange()">

and My current JS was:

function fileChange(){
  var file = document.getElementById('input_img');
  var imgname = document.getElementById('name');
  var form = new FormData();
  form.append("image", file.files[0])

    
    var settings = {
      "url": "https://api.imgbb.com/1/upload?key=(API CODE)",
      "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);
    
    
    });
}

when i upload image it will upload with the image default name but i want to give it custom name.
Current Result:
https://i.stack.imgur.com/bbKZb.jpg

What I want:
https://i.stack.imgur.com/9BesZ.jpg

CodePudding user response:

According to the api document.

name (optional) The name of the file, this is automatically detected if uploading a file with a POST and multipart / form-data

You should add &name=yourName to the url to chanage upload name

var settings = {
  "url": "https://api.imgbb.com/1/upload?key=(API CODE)&name="  $('#name').val(),
  "method": "POST",
  "timeout": 0,
  "processData": false,
  "mimeType": "multipart/form-data",
  "contentType": false,
  "data": form
};
  • Related