我有一个上传按钮,当我上传图片时,它会预览下面的图片。 我想得到图像的值并将其存储在var中。 ( 'example.png')。 如果上传新文件,则需要更改。
额外:无论如何我可以上传多张图片吗?如果我想上传最多4个?
var loadFile = function (event) {
var reader = new FileReader();
reader.onload = function () {
var output = document.getElementById('output');
output.src = reader.result;
};
reader.readAsDataURL(event.target.files[0]);
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- File name - Image -->
<input name="image-upload" id="image-upload" type="file" accept="image/*" onchange="loadFile(event)">
<br>
<br>
<img id="output"/>
答案 0 :(得分:0)
您可以尝试将文件名存储在调用此函数的变量中:
var storeFileName = function () {
var files = [];
var upload = document.getElementById("image-upload");
upload.onchange = function() {
for (var i = 0; i < upload.files.length; i++) {
files.push(upload.files[i].fileName);
}
}
}