【问题标题】:Flask file upload , The method is not allowed for the requested URLFlask 文件上传,请求的 URL 不允许该方法
【发布时间】:2017-12-14 08:53:53
【问题描述】:
I am trying to upload file. the abnormal is 'The method is not allowed for the requested URL' after run .  However my code is request methods is 'POST'

这是我的html:

<form action="" enctype='multipart/form-data' method="POST">
<span class="btn btn-info" id="import">
    <input type="file" name="file">
    </span>input type="submit" value="upload" id="submit">

这是我的 api 代码:

@api.route('/upload', methods=['POST','GET'])
def upload():
    if request.method == 'POST':
        file = request.files['file']
        upload_path = os.path.join(APP_STATIC_TXT, secure_filename(file.filename))
        file.save(upload_path)
        return redirect(url_for('/'))

本次运行结果:

信息:werkzeug:127.0.0.1 - - [14/Dec/2017 16:26:42] “POST /api/v1.0/上传 HTTP/1.1" 400 - INFO:werkzeug:127.0.0.1 - - [14/Dec/2017 16:26:42] "POST / HTTP/1.1" 405 - INFO:werkzeug: * 检测到变化 '/home/python/Desktop/VCG/vcg/api_1_0/test01.py', 重新加载

这是网页 html:

Method Not Allowed

请求的 URL 不允许该方法。

【问题讨论】:

  • 请在此处包含代码并正确格式化您的问题

标签: python file flask upload


【解决方案1】:

在您的form action 属性中使用current view url。请将其更改为url_for('upload')

【讨论】:

  • 改成 /upload 后:还是不行,异常是 ERROR:werkzeug:Error on request: and File "/usr/lib/python2.7/BaseHTTPServer.py", line 412, in send_header 和 self.wfile.write("%s: %s\r\n" % (keyword, value)) 和 IOError: [Errno 32] Broken pipe。
  • 你能打印file吗?
  • 无法打印文件
  • 你能在if request.method == 'POST'之后打印request.files吗?
  • 这是打印内容:ImmutableMultiDict([])
猜你喜欢
  • 2016-04-23
  • 2014-01-08
  • 2016-12-12
  • 2020-07-30
  • 2017-01-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 2021-08-16
相关资源
最近更新 更多