尝试将文件上传到服务器,但ImmutableMultiDict对象返回空。
upload.html
<html> <head> <title>Upload File Ajax</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript"> $(function() { $('#upload-file-btn').click(function() { var form_data = new FormData($('#upload-file')[0]); form_data.append('file', $('input[type=file]')[0].files[0]); console.log(form_data) $.ajax({ url: '/uploadajax', data: form_data, type: 'POST', contentType: false, cache: false, processData: false, async: false, success: function(data) { console.log('Success!'); // console.log(data); }, }); }); }); </script> </head> <body> <form action="" name="upload-file" id="upload-file" method="post" enctype="multipart/form-data"> <fieldset> <label for="file">Select a file</label> <input name="file" id="file" type="file"> </fieldset> <fieldset> <button id="upload-file-btn" type="button">Upload</button> </fieldset> </form> </body> </html>
烧瓶
app = Flask(__name__) @app.route('/') def index(): return render_template("upload.html") @app.route('/uploadajax', methods = ['POST']) def upldfile(): logging.debug('request.method : %s', request.method) logging.debug('request.files : %s', request.files) logging.debug('request.args : %s', request.args) logging.debug('request.form : %s', request.form) logging.debug('request.values : %s', request.values) logging.debug('request.headers : %s', request.headers logging.debug('request.data : %s', request.data) upload_files = request.files.getlist("file") logging.debug('upload_files : %s', upload_files) file_val = request.files for k, v in file_val.items() :logging.debug('key : %s -- value : %s ', k, v) # if request.method == 'POST':file_val = request.files['file'] return render_template('upload.html') if __name__ == '__main__': app.run(host='127.0.0.1', debug=True, port=2345, use_reloader=True)
这是日志数据
016-05-03 00:05:30,500 - root - DEBUG - request.method : POST 2016-05-03 00:05:30,500 - root - DEBUG - request.files : ImmutableMultiDict([]) 2016-05-03 00:05:30,500 - root - DEBUG - request.args : ImmutableMultiDict([]) 2016-05-03 00:05:30,501 - root - DEBUG - request.form : ImmutableMultiDict([]) 2016-05-03 00:05:30,501 - root - DEBUG - request.values : CombinedMultiDict([ImmutableMultiDict([]), ImmutableMultiDict([])]) 2016-05-03 00:05:30,501 - root - DEBUG - request.headers : Referer: http://127.0.0.1:2345/ Origin: http://127.0.0.1:2345 Content-Length: 368 User-Agent: Mozilla/5.0.... Connection: keep-alive X-Requested-With: XMLHttpRequest Host: 127.0.0.1:2345 Accept: */* Accept-Language: en-US,en;q=0.8 Content-Type: false Accept-Encoding: gzip, deflate2016-05-03 03:13:58,236 - root - DEBUG - request.data : ------WebKitFormBoundaryA8jmfBTRKGJFtMS Content-Disposition: form-data; name="file"; filename="test_upload.txt" Content-Type: text/plain qbcdefgh abcdefgh ------WebKitFormBoundaryA8jmfBTRKGJFtMS Content-Disposition: form-data; name="file"; filename="test_upload.txt" Content-Type: text/plain qbcdefgh abcdefgh ------WebKitFormBoundaryA8jmfBTRKGJFtMS-- 2016-05-03 00:05:30,501 - root - DEBUG - upload_files : []
您的jQuery版本太旧,无法引起此情况。
jQuery
当我使用 2.1.1时 ,代码运行正常。
顺便说一句,您不需要form_data.append。
form_data.append