最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Attach image and send it via json in base64 encoding - Stack Overflow

programmeradmin3浏览0评论

I need to create the ability to attach pictures and send them via JSON in base64. But I can't get the resulting string from FileReader object. I will be very grateful if you can tell me how to fix this.

<form>
    <label>Picture
         <input type="file" accept=".jpg, .jpeg, .png" name="picture" required>
    </label>
</form>
async function push() {
// some other actions here
    let form = new FormData(document.querySelector('form'));
    let json = construct_json(form);
//calls  of other functions
}

function getBase64(file) {
   let reader = new FileReader();
   reader.readAsDataURL(file);
   reader.onload = function () { // I think there is a mistake here.
     reader.result;
   };
}

function construct_json(form) {
    let json = {
        picture: getBase64(form.get('picture')),
    };

    return JSON.stringify(json);
}

UPD: If I'm trying to use json in the push() function then I get the same problem. And adding await doesn't help. I would be grateful for a hint how can I display json in the push() function?

I need to create the ability to attach pictures and send them via JSON in base64. But I can't get the resulting string from FileReader object. I will be very grateful if you can tell me how to fix this.

<form>
    <label>Picture
         <input type="file" accept=".jpg, .jpeg, .png" name="picture" required>
    </label>
</form>
async function push() {
// some other actions here
    let form = new FormData(document.querySelector('form'));
    let json = construct_json(form);
//calls  of other functions
}

function getBase64(file) {
   let reader = new FileReader();
   reader.readAsDataURL(file);
   reader.onload = function () { // I think there is a mistake here.
     reader.result;
   };
}

function construct_json(form) {
    let json = {
        picture: getBase64(form.get('picture')),
    };

    return JSON.stringify(json);
}

UPD: If I'm trying to use json in the push() function then I get the same problem. And adding await doesn't help. I would be grateful for a hint how can I display json in the push() function?

Share Improve this question edited Aug 4, 2020 at 7:37 Helen asked Aug 3, 2020 at 10:15 HelenHelen 5433 gold badges11 silver badges26 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Updating the answer based on ment. you can refer to the example on sandbox

Please add create a promise to avoid callback hell. Here I have promised the base46 Function. Promisification

const getBase64Promise = function (file) {
  return new Promise((resolve, reject) => {
    getBase64(file, (success,err) => {
        if(err) reject(err)
        else resolve(success);
    });
  });
};

Like this

async function construct_json(form, callback) {
  let data = await getBase64Promise(form.get("picture"));
    let json = {
      picture: data
    };
    return json;
}

Yes. You are making a mistake when reader.onload method gets envoke. You can find example here

You forget to addEventListener and callback when upload gets done. Please add the following code

window.addEventListener("change", push);

function getBase64(file, callback) {
  let reader = new FileReader();
  reader.onload = function(e) {
    // console.log(e.target);
    // I think there is a mistake here

    callback(e.target.result);
  };
  reader.onloadend = function(e) {
    return e.target.result;
  };
  return reader.readAsDataURL(file);
}

async function construct_json(form) {
  await getBase64(form.get("picture"), data => {
    let json = {
      picture: data
    };

    return JSON.stringify(json);
  });
}
发布评论

评论列表(0)

  1. 暂无评论