-1
我想创建名为“images”的新文件夹,并在“images”文件夹中保存文件[来自upload.html]。我的问题是,文件夹正在生成,但文件没有保存到文件夹中。请参阅下面附带的代码。烧瓶,Python:文件没有保存在文件夹“images”
upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Convert csv to JSON</title>
</head>
<body>
<form action="{{url_for('upload')}}" method="post" enctype="multipart/form-data">
<input type="file" name="upload" accept="image/"/>
<input type="submit" /></form>
</body>
</html>
控制台出套上
是:
E:/images
<FileStorage: u'I-94.pdf' ('application/pdf')>
('Accept incoming file:', u'I-94.pdf')
E:/images/I-94.pdf
File "E:\datamining\tryproject\convert.py", line 32, in upload
upload.save(destination)
AttributeError: 'function' object has no attribute 'save'
Python代码如下所示
convert.py
import os
from flask import Flask, request, render_template, send_from_directory
__author__ = 'praveen'
app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
@app.route("/")
def index():
return render_template("upload.html")
@app.route("/upload", methods=["POST"])
def upload():
target = os.path.join(APP_ROOT, '/images')
print(target)
if not os.path.isdir(target):
os.mkdir(target)
for file in request.files.getlist("upload"):
print(file)
filename = file.filename
destination = "/".join([target, filename])
print("Accept incoming file:", filename)
print(destination)
upload.save(destination)
if __name__ == "__main__":
app.run(port=4555, debug=True)
'upload'是您的视图函数的名称,而不是文件对象。 – stamaimer
@stamaimer谢谢你的建议。现在工作正常。 – praveen
@eyllanesc我做了更正并发布了正确答案。 – praveen