javascript - 我应该如何将图像从用户发送到带有 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 如何工作或如何使用它们。
解决方案
推荐阅读
- reactjs - React Redux - 存储登录用户的详细信息
- swift - 如何在 Xcode 的 iOS 项目中设置 AWS 移动 SDK
- python - 使用 Pyramid - 如何获取带有挂载点的 host_url?
- angular - Angular 5 结构指令
- sql-server - 未找到数据源名称且未指定默认驱动程序 nodejs
- java - IntelliJ IDEA Alt+Scroll Wheel 在 Windows 中停止工作
- graphql - $util.transform.toDynamoDBFilterExpression
- javascript - 数组结果,在输入中用于自动完成
- java - MPAndroidChart 和数据绑定
- javascript - 当用户选择“不再询问”并禁用地理定位时,React native `Geolocation.getCurrentPosition` 承诺将永远不会被解决/拒绝