首页 > 解决方案 > 我应该如何将图像从用户发送到带有 html5 js 的目录?

问题描述

所以我试图设置一个东西,我从用户那里获取一个带有 html 文件输入的图像,然后在选择它时预览给定的图像。现在,我想将给定的图像发送到一个目录,显示它,然后删除该文件。我知道如何在 node.js 中执行此操作,但由于我无法在浏览器中使用 node.js,所以我不知道该怎么做。以下是我在 node.js 中的处理方式:

window.onload = function(e) {
  const chooseImg = document.getElementById("myFile"), img = document.getElementById('imgPreview');
  //Event Listener
  chooseImg.addEventListener("change", function(ev) {
    //get the file name
    let imgPath = chooseImg.value.split('\\')[2];
    //Create the file in ./_temp/
    fs.open(`./_temp/${imgPath}`, 'w', function (err, file) {
      if (err) throw err;
    });
    //Display the image
    img.src = `./_temp/${imgPath}`;
    //Delete the file
    fs.unlinkSync(`./_temp/${imgPath}`);
  }, false);
}

chooseImg是我的标签,img<img>我想要显示图像的元素。我想我需要使用 Blob,但我无法弄清楚 Blob 如何工作或如何使用它们。

标签: javascripthtml

解决方案


推荐阅读