这篇文章主要介绍了jquery ajax使用formdata上传文件和其他数据后端web.py获取 ,需要的朋友可以参考下,希望能帮助到大家。
XMLHttpRequest Level 2 添加了一个新的接口——FormData。与普通的 Ajax 相比,使用 FormData 的最大优点就是我们可以异步上传二进制文件。
jQuery 2.0+的版本支持FormData
方法一:使用
<script> function loadFile(file){ var formdata = new FormData($$('form')[0]); $.ajax({ url: 'jobs/add', type: 'POST', datatype: 'json', data: formdata, cache:false, traditional: true, contentType: false, processData: false, success: function (data) {}, error: function () {} }); } </script>•后台(web.py):
class Add: def POST(self): i = web.input(myfile={}) print(i['myfile'].filename) #文件名 print(i['myfile'].value) #文件内容 print(i['myfile'].file.read()) #文件内容
注意:
1.
以上就是jQuery Ajax使用FormData上传文件和其他数据实例详解的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号