大家好我有一个简单的表单,上面有这样的上传按钮。
<form role="form" id="sf" name = "sf" method="post"
enctype="multipart/form-data" action="javascript:void();">
<input type="file" id="file" name="file" class="file">
</form>
我的js看起来像这样。
$('#file').bind('change', function() {
var form_data = new FormData($('#suject-form')[15]);
var request = $.ajax({
url: "/upload",
type: "POST",
contentType: "application/json",
cache: false,
processData: false,
async: false,
data: {
file: new FormData($('#suject-form')[0]),
state: 'testing'
},
dataType: "json",
})
.done(function(data){
console.log(data);
})
return false;
});
- 当我尝试获取文件数据时,它一直给我400错误,
-
@app.route('/upload', methods=['POST'])
def upload():
if request.method == 'POST':
state= request.form['state']
files = request.files['file']
# if I leave out file it returns but fails when leave in files
return jsonify(result=[state])
任何人都知道我做错了什么?谢谢!