当前位置: 首页 > 知识库问答 >
问题:

Flask:创建没有表单的上传服务器[重复]

东郭良弼
2023-03-14

我写了一个小烧瓶服务器。目的是上传文件。重要的是,这是一个只有REST API的服务器,不呈现html和模板文件。

这是代码:

from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/', methods=['POST'])
def upload_file():
    uploaded_file = request.files['file']
    if uploaded_file.filename != '':
        uploaded_file.save(uploaded_file.filename)
    return redirect(url_for('index'))
    
if __name__ == "__main__":
    app.run(debug=True)
curl -X POST -d file=/home/user1/Desktop/STEP_files/MyFile.txt 127.0.0.1:5000
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/werkzeug/datastructures.py", line 442, in __getitem__
    raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/user1/Desktop/flaskServer/server.py", line 11, in upload_file
    uploaded_file = request.files['file']
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/debughelpers.py", line 100, in __getitem__
    raise DebugFilesKeyError(request, key)
flask.debughelpers.DebugFilesKeyError: You tried to access the file "file" in the request.files dictionary but it does not exist.  The mimetype for the request is "application/x-www-form-urlencoded" instead of "multipart/form-data" which means that no file contents were transmitted.  To fix this error you should provide enctype="multipart/form-data" in your form.

The browser instead transmitted some file names. This was submitted: "/home/user1/Desktop/STEP_files/MyFile.txt"
127.0.0.1 - - [02/Feb/2021 16:49:37] "POST / HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/user1/Desktop/flaskServer/server.py", line 11, in upload_file
    uploaded_file = request.files['file']
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/werkzeug/datastructures.py", line 442, in __getitem__
    raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'file'

如果执行curl-x post-d“file=@/home/user1/desktop/step_files/myfile.txt”127.0.0.1:5000

我得到:

127.0.0.1 - - [02/Feb/2021 16:53:11] "POST / HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/debughelpers.py", line 96, in __getitem__
    return oldcls.__getitem__(self, key)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/werkzeug/datastructures.py", line 442, in __getitem__
    raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/user1/Desktop/flaskServer/server.py", line 11, in upload_file
    uploaded_file = request.files['file']
  File "/home/user1/anaconda3/envs/flaskTest/lib/python3.9/site-packages/flask/debughelpers.py", line 100, in __getitem__
    raise DebugFilesKeyError(request, key)
flask.debughelpers.DebugFilesKeyError: You tried to access the file "file" in the request.files dictionary but it does not exist.  The mimetype for the request is "application/x-www-form-urlencoded" instead of "multipart/form-data" which means that no file contents were transmitted.  To fix this error you should provide enctype="multipart/form-data" in your form.

The browser instead transmitted some file names. This was submitted: "@/home/user1/Desktop/STEP_files/Assembly_Test.step"

共有1个答案

冯宏浚
2023-03-14

这里的问题是Curl命令,而不是Flask实现,使用-f发送多部分/表单数据请求,使用@snytax Accaccy发送文件,而不是将文件名作为字符串。

curl -X POST -F file=@/home/user1/Desktop/STEP_files/MyFile.txt 127.0.0.1:5000
 类似资料:
  • 假定用户需要创建的Web服务 服务名称 版本号 监听地址 网络类型 读取请求数据超时 写入响应数据超时 myapp 1.0 0.0.0.0:8080 http 0 0 无版本号的服务 func main() { ... app := faygo.New("myapp") ... 注:相应的配置文件为config/myapp.ini 含版本号的服务 func main() { ...

  • 创建 http 服务器 package main   import ( "net/http"   "github.com/hprose/hprose-golang/rpc" )   func hello(name string) string { return "Hello " + name + "!" }   func main() { service := rp

  • 我对编写Flask API真的很陌生。有没有办法将Android设备上录制的音频文件发送到我的flask服务器?我应该逐字节发送音频文件,还是有办法直接发送?

  • 我一直从中得到: “404未找到-未找到”。 在服务器上找不到请求的URL。如果您手动输入URL,请检查拼写,然后再试一次。我肯定有一个愚蠢的错误,我是python新手,我花了几个小时试图理解这个问题。 我的代码: 注册格式为: 我确认我可以直接联系http://127.0.0.1:5000/register. 在调试模式下运行应用程序,这就是我得到的: 使用stat重新启动 调试器处于活动状态

  • 为什么我的文件上载到而不是。并将所有二进制文件数据解析为unicode字符串。 接下来,我在Python请求中进行测试。以下代码: 而服务器端输出则相反:

  • 本文向大家介绍轻松创建nodejs服务器(10):处理上传图片,包括了轻松创建nodejs服务器(10):处理上传图片的使用技巧和注意事项,需要的朋友参考一下 本节我们将实现,用户上传图片,并将该图片在浏览器中显示出来。 这里我们要用到的外部模块是Felix Geisendörfer开发的node-formidable模块。它对解析上传的文件数据做了很好的抽象。 要安装这个外部模块,需在cmd下执

  • 我想创建一个Apache服务器,该服务器可以通过其IP地址访问,以托管应用程序的资源,并且我使用的是XAMPPV3。2.2这样做。当我通过可通过控制面板访问的服务器本地IP访问从连接到同一网络(或本地网络,即服务器)的任何计算机托管的网页时,服务器主机正常- 编辑: 以下是我的XAMPP Apache的配置: httpd.conf:

  • 假定用户需要创建的Web服务 服务名称 版本号 监听地址 网络类型 读取请求数据超时 写入响应数据超时 myapp1 1.0 0.0.0.0:8080 http 0 0 myapp2 2.0 0.0.0.0:8081 http 0 0 无版本号的服务 func main() { ... app1 := faygo.New("myapp1") app2 := faygo.New("mya