![](/img/trans.png)
[英]Where to find the file after uploading via Flask/Python/Heroku?
[英]Flask - Uploading a file via Curl
我正在尝试通过 curl 将文件上传到我的 flask 应用程序。 我没有收到任何错误,但 curl 命令最终发送了一个空白文件,或者 flask 代码无法正确读取它。
以下是flask代码:
#Upload a new set of instructions for <ducky_name>.
@app.route('/upload/instructions/<ducky_name>/', methods = ['POST'])
def upload_Instruction(ducky_name):
file = request.data
print("file: ", file)`
path = os.getcwd() +/files/" + ducky_name + ".txt"
with open(path, "w") as f:
f.write(file)
print("f: ", f)
f.close()
return "Success"
而下面的 curl 命令是:
curl -X POST -d @test.txt http://127.0.0.1:5000/upload/instructions/test1/
这是目录树:
├── README.md
└── server_app
├── app
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── __pycache__
│ │ └── __init__.cpython-36.pyc
│ ├── routes.py
│ └── routes.pyc
├── files
│ ├── test1
│ ├── test1.txt
│ └── test.txt
├── __pycache__
│ └── server_app.cpython-36.pyc
├── server_app.py
└── server_app.pyc
file = request.data
行有问题吗?
试试这个改变,让我知道它是否有效
在“app = Flask( name )”之后添加这两行:
UPLOAD_FOLDER = 'files'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
对您的路线进行以下更改:
@app.route("/upload/instructions/<ducky_name>", methods=["POST"])
def post_file(ducky_name):
"""Upload a file."""
file = request.files['secret']
file.save(os.path.join(app.config['UPLOAD_FOLDER'], ducky_name))
# Return 201 CREATED
return "", 201
其中 test.txt 是您要上传的文件的原始名称,newname.txt 是您上传后要保存的文件的名称
curl -F secret=@test.txt http://127.0.0.1:5000/upload/instructions/newname.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.