我正在尝试使用文件input
和jquery
发送多张照片。我试过了 -
//html part=============
<div class="form-group">
<input class="button" type="file" name="files" multiple />
</div>
<button id="AddMorePhoto">Add more photo </button>
//JQuery part ============
$('#AddMorePhoto').on('click', function (e) {
e.preventDefault();
$('<div/>').addClass('form-group')
.html($('<input class="multiplephoto" type="file" name="files" multiple />')).insertBefore(this);
});
//Submitting to server=================
var formdata= new FormData();
var fileInput = $('.multiplephoto');
for (var i = 0; i < fileInput.length; i++) {
formdata.append('photos',fileInput[i]); //no photo appending here
}
但照片未附加到formdata
。有什么帮助吗?
答案 0 :(得分:0)
尝试使用此代码:
$(document).on("click", "submit", function (e) {
e.preventDefault();
var inputs = $(".form-group input[type=file]");
$.each(inputs, function (obj, f) {
for(var i=0; i<f.files.length; i++){
myFormData.append('photo', f.files[i]);
}
});
});