Python 和 Flask - 尝试使用函数 return 文件内容
Python and Flask - Trying to have a function return a file content
我正在努力 return 将文件内容返回给用户。有一个从用户那里接收 txt 文件的 Flask 代码,然后调用 Python 函数 transform() 来解析 infile,两个代码都在做这项工作。
当我尝试将新文件 (outfile) 发送回 (return) 给用户时出现问题,该文件的 Flask 代码也工作正常。
但是我不知道如何让这个 Python transform function() "return" 文件内容,已经测试了几个选项。
更多详情如下:
def transform(filename):
with open(os.path.join(app.config['UPLOAD_FOLDER'],filename), "r") as infile:
with open(os.path.join(app.config['UPLOAD_FOLDER'], 'file_parsed_1st.txt'), "w") as file_parsed_1st:
p = CiscoConfParse(infile)
'''
parsing the file uploaded by the user and
generating the result in a new file(file_parsed_1st.txt)
that is working OK
'''
with open (os.path.join(app.config['UPLOAD_FOLDER'], 'file_parsed_1st.txt'), "r") as file_parsed_2nd:
with open(os.path.join(app.config['UPLOAD_FOLDER'], 'file_parsed_2nd.txt'), "w") as outfile:
'''
file_parsed_1st.txt is a temp file, then it creates a new file (file_parsed_2nd.txt)
That part is also working OK, the new file (file_parsed_2nd.txt)
has the results I want after all the parsing;
Now I want this new file(file_parsed_2nd.txt) to "return" to the user
'''
#Editing -
#Here is where I was having a hard time, and that now is Working OK
#using the follwing line:
return send_file(os.path.join(app.config['UPLOAD_FOLDER'], 'file_parsed_2nd.txt'))
您确实需要使用 flask.send_file()
callable 来产生正确的响应,但需要传入 尚未关闭或即将关闭的文件名或文件对象。所以传递完整路径会做:
return send_file(os.path.join(app.config['UPLOAD_FOLDER'], 'file_parsed_2nd.txt'))
当您传入一个文件对象时,您不能使用 with
语句,因为它会在您从视图中 return 时关闭文件对象;只有当响应对象被处理为 WSGI 响应时,它才会被实际读取,在你的视图函数之外。
如果您想向浏览器建议一个文件名以将文件另存为,您可能需要传入一个 attachment_filename
参数;它还将有助于确定模仿类型。您可能还想使用 mimetype
参数明确指定 mimetype。
您也可以使用 flask.send_from_directory()
function;它的作用相同,但需要一个文件名和一个目录:
return send_from_directory(app.config['UPLOAD_FOLDER'], 'file_parsed_2nd.txt')
关于 mimetype 的警告同样适用;对于 .txt
,默认的 mimetype 将是 text/plain
。该函数实质上连接了目录和文件名(使用 flask.safe_join()
应用额外的安全检查以防止使用 ..
构造突破目录)并将其传递给 flask.send_file()
.
我正在努力 return 将文件内容返回给用户。有一个从用户那里接收 txt 文件的 Flask 代码,然后调用 Python 函数 transform() 来解析 infile,两个代码都在做这项工作。
当我尝试将新文件 (outfile) 发送回 (return) 给用户时出现问题,该文件的 Flask 代码也工作正常。 但是我不知道如何让这个 Python transform function() "return" 文件内容,已经测试了几个选项。
更多详情如下:
def transform(filename):
with open(os.path.join(app.config['UPLOAD_FOLDER'],filename), "r") as infile:
with open(os.path.join(app.config['UPLOAD_FOLDER'], 'file_parsed_1st.txt'), "w") as file_parsed_1st:
p = CiscoConfParse(infile)
'''
parsing the file uploaded by the user and
generating the result in a new file(file_parsed_1st.txt)
that is working OK
'''
with open (os.path.join(app.config['UPLOAD_FOLDER'], 'file_parsed_1st.txt'), "r") as file_parsed_2nd:
with open(os.path.join(app.config['UPLOAD_FOLDER'], 'file_parsed_2nd.txt'), "w") as outfile:
'''
file_parsed_1st.txt is a temp file, then it creates a new file (file_parsed_2nd.txt)
That part is also working OK, the new file (file_parsed_2nd.txt)
has the results I want after all the parsing;
Now I want this new file(file_parsed_2nd.txt) to "return" to the user
'''
#Editing -
#Here is where I was having a hard time, and that now is Working OK
#using the follwing line:
return send_file(os.path.join(app.config['UPLOAD_FOLDER'], 'file_parsed_2nd.txt'))
您确实需要使用 flask.send_file()
callable 来产生正确的响应,但需要传入 尚未关闭或即将关闭的文件名或文件对象。所以传递完整路径会做:
return send_file(os.path.join(app.config['UPLOAD_FOLDER'], 'file_parsed_2nd.txt'))
当您传入一个文件对象时,您不能使用 with
语句,因为它会在您从视图中 return 时关闭文件对象;只有当响应对象被处理为 WSGI 响应时,它才会被实际读取,在你的视图函数之外。
如果您想向浏览器建议一个文件名以将文件另存为,您可能需要传入一个 attachment_filename
参数;它还将有助于确定模仿类型。您可能还想使用 mimetype
参数明确指定 mimetype。
您也可以使用 flask.send_from_directory()
function;它的作用相同,但需要一个文件名和一个目录:
return send_from_directory(app.config['UPLOAD_FOLDER'], 'file_parsed_2nd.txt')
关于 mimetype 的警告同样适用;对于 .txt
,默认的 mimetype 将是 text/plain
。该函数实质上连接了目录和文件名(使用 flask.safe_join()
应用额外的安全检查以防止使用 ..
构造突破目录)并将其传递给 flask.send_file()
.