Skip to content

Instantly share code, notes, and snippets.

@shuiRong
Created February 1, 2018 07:25
Show Gist options
  • Save shuiRong/f2bb34d48046bcd5eb6d1307a2713e97 to your computer and use it in GitHub Desktop.
Save shuiRong/f2bb34d48046bcd5eb6d1307a2713e97 to your computer and use it in GitHub Desktop.
JS,Ajax上传图片(二进制数据)
$('...').on('change', function () {
var filearr = [];
var myfile = document.getElementById('file');
var files = this.files;
var formData = new FormData();
for (var i = 0; i < files.length; i++) {
//提交时,我们把filearr中的数据遍历一遍
console.log(i, files[i]);
formData.append('file', files[i]); //用append添加到formData中
}
$.ajax({
url: '',
type: 'POST',
data: formData,
cache: false, //不设置缓存
processData: false, // 不处理数据
contentType: false, // 不设置内容类型
success: function(res) {
$('#head-img-base64').val(res);
},
error: function(e) {
console.log(e);
}
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment