Last active
August 29, 2015 14:17
-
-
Save libo1106/52e6176096eaef5c0ce8 to your computer and use it in GitHub Desktop.
Ajax 上传文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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