gpt4 book ai didi

python - 从 jQuery 请求数据中获取一个空的 ImmutableMultiDict 对象

转载 作者:太空宇宙 更新时间:2023-11-04 05:31:30 28 4
gpt4 key购买 nike

尝试将文件上传到服务器,但 ImmutableMultiDict 对象返回空。

上传.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>

flask

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 版本太旧导致的。

当我使用 2.1.1 时,代码运行正常。

顺便说一句,您不需要form_data.append

关于python - 从 jQuery 请求数据中获取一个空的 ImmutableMultiDict 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36995516/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com