Благодаря комментаторам этого поста я смог исправить это. Первоначально я получал b " как результат только после использования only f.read()
.
Когда я использовал f.seek(0)
, я получил вывод моего текста файл, но с множеством ошибок кодирования и форматирования, включая new , предшествующий b`, за которым следует мой вывод.
Но добавление content = f.read()
content = str(content, 'utf-8')
также исправило большую часть этого .
Итак, вот окончательное решение -
def upload_source():
if request.method == 'POST':
# check if the post request has the file part
f = request.files['file']
if f.filename == "":
print("No file Name")
return redirect(request.url)
if not allowed_file(f.filename):
print("File extension not allowed!")
return redirect(request.url)
else:
full_filename = secure_filename(f.filename)
f.save(os.path.join(app.config['UPLOAD_FOLDER'], full_filename))
f.seek(0)
print("File saved")
content = f.read()
content = str(content, 'utf-8')
return render_template('source.html', text=content)
и не забудьте все тот же старый HTML -
<p> {{ text }} </p>
Надеюсь, это поможет и другим. Спасибо!