我采用了非犹太方法并使用了$.ajax()
ajax 文件上传,因为目前我无法弄清楚“骨干”方式。图片已上传,ajax 请求以 JSON 格式发回新的主干模型,其中填充了新照片的文件名。但是模型没有触发change
事件,所以我无法填充个人资料图片。请参阅success
下面的函数。
getFile:function(e){
e.preventDefault();
that = this;
var file = e.target.files[0];
name = file.name;
size = file.size;
type = file.type;
var formData = new FormData($('#upload-child-pic-form')[0]);
console.log(this,'currentUploadU',this.currentUpload);
formData.append('child_id',this.currentUpload);
$.ajax({
url: '/children/upload/',
type: 'POST',
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){
myXhr.upload.addEventListener('progress',function(data){
$("#upload-child-pic-progress").val(data.position / data.totalSize * 100);
}, false);
}
return myXhr;
},
//Ajax events
beforeSend: function(data){
$("#upload-child-pic-progress").removeClass('hide');
},
success: function(data){
$("#upload-child-pic-progress").addClass('hide');
$("#add-child-profile-pics-modal").modal('hide');
var sentData = $.parseJSON(data);
var thisChild = that.children.get(sentData.id);
thisChild.set("photo",sentData.photo);
thisChild.trigger('change');
},
error: function(data){
console.log('error',data);
},
// Form data
data: formData,
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});
},