文章内容

2018/3/17 10:32:29,作 者: 黄兵

The browser (or proxy) sent a request that this server could not understand.

最近在使用Flask的时候,报错,内容如下:

The browser (or proxy) sent a request that this server could not understand.

检查了一下前端代码,内容如下:

<div class="tab-pane" id="tab_1_2">
<p> 请上传小于3M的文件。 </p>
<form action="/manage/file_upload" role="form" enctype=multipart/form-data method="post">
<div class="form-group">
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-new thumbnail" style="width: 200px; height: 150px;">
<img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&amp;text=no+image" alt=""/>
</div>
<div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;"></div>
<div>
<span class="btn default btn-file">
<span class="fileinput-new"> 选择图片 </span>
<span class="fileinput-exists"> Change </span>
<input type="file" name="file">
</span>
<a href="javascript:;" class="btn default fileinput-exists" data-dismiss="fileinput"> Remove </a>
</div>
</div>
<div class="clearfix margin-top-10">
<span class="label label-danger">NOTE! </span>
<span>Attached image thumbnail is supported in Latest Firefox, Chrome, Opera, Safari and Internet Explorer 10 only </span>
</div>
</div>
<div class="margin-top-10">
<button class="btn green" type="submit">Submit</button>
<a href="javascript:;" class="btn default"> Cancel </a>
</div>
</form>
</div>

可以看到input type="file" name="..."这段代码的name值是“...”,我们再看一下后端代码:

@manage.route('/file_upload', methods=['GET', 'POST'])
@login_required
def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(os.environ.get['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file', filename=filename))
    return 'ok'

这一句:request.files['file'],没有取到前端的值,更改前端代码如下:

<div>
<span class="btn default btn-file">
<span class="fileinput-new"> 选择图片 </span>
<span class="fileinput-exists"> Change </span>
<input type="file" name="file">
</span>
<a href="javascript:;" class="btn default fileinput-exists" data-dismiss="fileinput"> Remove </a>
</div>

之后再次测试,问题解决。


黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - The browser (or proxy) sent a request that this server could not understand.

分享到:

发表评论

评论列表