描述: input上传多张照片, 专程base64格式上传
// html
<div>
<input id="file_input" type="file" onchange="previewImage(this, 'img_preview')" multiple>
<button onclick="upload">上传</button>
</div>
<div id="img_preview"></div>
// js
<script src="zepto.min.js"></script>
<script>
var images = [];
var uploaded = [];
var upload_phase;
// 图片转base64
function previewImage(el, previewId) {
images = [];
// 页面放图片的div
var preview = $('#'+previewId)
preview.empty()
function readAndPreview(file) {
if(/\.(jpe?g|png|gif)$/i.test(file.name)) {
var reader = new FileReader()
reader.addEventListener("load", function(){
var image = new Image();
image.height = 100l
image.title = file.name;
image.src = this.result;
// 多张图片放进数组中
images.push(this.result);
preview.append($(image).addclass("img-view"))
}, false)
reader.readAsDataURL(file)
})
}
if(el.files){
[].forEach.call(el.files, readAndPreview)
}
}
// 上传图片
function upload(phase){
uploaded = []
if($("file_input")[0].files[0] == undefined){
alert('INPUT EMPTY')
return;
}
[].forEach.call(images, (image) => {
var formData = new FormData()
formData.append("base64Image", image)
var url = "https://b612-fs.snowcam.cn/v1/event/vision/face/upload-only";
$.ajax({
type: "POST",
url: url,
data: formData,
dataType: "text",
processData: false,
contentType: false,
headers: {Accept: "*/*"},
timeout: 30 * 60 * 60 * 1000,
success: function (response) {
// success
},
error: function (e) {
console.log("ERROR : " + e.responseText);
}
});
})
}
</script>