Issue
I uploaded several files using the following codes. But I don't know how to call and use those files. Or where are these files stored?
In the following code snippet, I request the user to upload a file and I want to read those files after converting them to .csv format and save the information on a chart. But I don't know where my files are saved and I can't find the folder path.
from time import time
from xml.dom.minidom import DocumentType
import plotly.graph_objects as go
import pandas as pd
import os
from flask import Flask, flash, request, redirect, url_for
from werkzeug.utils import secure_filename # is explained a little bit later
upload_folder = '/path/to/the/uploads' #is where we will store the uploaded files
allowed_extensions = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'} # is the set of allowed file extensions
app = Flask(__name__)
app.config['upload_folder'] = upload_folder
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in allowed_extensions
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('download_file', name=filename))
return
HTML FORM : I assume that there is no connection between these two pieces of code. How can I make sure that the connection is established?
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>File Upload</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='main.css'>
<script src='main.js'></script>
</head>
<body>
<div class="container">
<div class="Headers">
<h1> Upload File </h1>
</div>
<div>
<form method="POST" action="" enctype="multipart/form-data">
<p><input type="file" name="file"></p>
<p><input type="submit" value="Submit"></p>
</form>
</div>
</div>
</body>
</html>
Solution
In the following line is where the file is saved
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
Therefore, you are saving the file to whatever this path is
app.config['UPLOAD_FOLDER']
Unless you are hiding the name of your file for the purpose of the question you currently have this set as '/path/to/the/uploads'
which will not work, you need to set it as an actual path in your directory, I assume you copied this from somewhere and forgot to change it.
If you create a folder called uploads
in your root directory then you could change it to
upload_folder = '/uploads'
The the file will be uploaded into that folder. With Flask it is common to have a folder called static for storing things like uploads. If you have that then you could change it to upload_folder = '/static/uploads'
, but the idea is the same.
Answered By - MarcusWilliams
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.