Skip to content

Instantly share code, notes, and snippets.

@libo1106
Last active August 29, 2015 14:17
Show Gist options
  • Save libo1106/52e6176096eaef5c0ce8 to your computer and use it in GitHub Desktop.
Save libo1106/52e6176096eaef5c0ce8 to your computer and use it in GitHub Desktop.
Ajax 上传文件
/**
* XHR2特性,使得ajax可以上传文件
**/
post_image: function(file, success){
if(!file){
return false;
}
var url = server + 'post_image';
var data_query = {
user_id : store.get('user_id'),
token : store.get('token')
};
// 由于要关闭jQuery的processData,估URL上面的额外query需要自己手动拼接
url += '&'+ $.param(data_query);
// 生成表单对象
var dataForm = new FormData();
// 填充表单字段
dataForm.append('upfile', file);
return $.ajax({
url: url,
method: 'POST',
data: dataForm,
contentType: false, // 告诉jQuery不要去设置Content-Type请求头
processData: false, // 告诉jQuery不要去处理发送的数据
success: success
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment