oceania
- 发布:2015-04-21 20:48
- 更新:2018-08-27 11:37
- 阅读:5169
mui.ajax支持文件上传和普通参数混合提交。参考以下代码
var fd = new FormData();
fd.append("username", "mui");//普通参数
fd.append('file', file.files[0]);//文件
//目前需要使用标准xhr。plus的xhr暂不支持
mui.ajaxSettings.xhr = function(protocol) {
return new window.XMLHttpRequest();
};
mui.ajax({
url: 'script.php',
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data) {
alert(data);
}
});
完整的测试代码
<div class="mui-content mui-fullscreen">
<input type="file" name="file" id="file" style="margin: 50px 0;"/>
<button id="upload" class="mui-btn mui-btn-block mui-btn-primary">上传</button>
</div>
<script type="text/javascript" src="js/mui.min.js"></script>
<script type="text/javascript" charset="utf-8">
mui.init();
var fileEl = document.getElementById("file");
document.getElementById("upload").addEventListener('tap', function() {
//标准xhr可以正常提交文件,plus的xhr提交的文件为空
mui.ajaxSettings.xhr = function(protocol) {
return new window.XMLHttpRequest();
};
var fd = new FormData();
fd.append("username", "mui");
fd.append('userfile', file.files[0]);
mui.ajax({
url: 'http://192.168.1.117/loccitane/admin/index.php/api/post',
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data) {
alert(data);
}
});
});
</script>
oceania (作者)
请问file.files[0]的file是指什么?
2015-04-22 23:26