Read file data without saving it in Flask
FileStorage
contains stream
field. This object must extend IO or file object, so it must contain read
and other similar methods. FileStorage
also extend stream
field object attributes, so you can just use file.read()
instead file.stream.read()
. Also you can use save
argument with dst
parameter as StringIO
or other IO or file object to copy FileStorage.stream
to another IO or file object.
See documentation: http://flask.pocoo.org/docs/api/#flask.Request.files and http://werkzeug.pocoo.org/docs/datastructures/#werkzeug.datastructures.FileStorage.
How to read uploaded file data without saving it in Flask?
If you want only to read data from file then simply use .read()
uploaded_file = request.files.get('uploaded_file')
data = uploaded_file.read()
print(data)
And if you want to use it with function which needs filename then usually it may work also with file-like
object and you can use it directly
ie. Pillo.Image
from PIL import Image
uploaded_file = request.files.get('uploaded_file')
img = Image.open(uploaded_file)
img.save('new_name.jpg')
ie. pandas.DataFrame
import pandas as pd
uploaded_file = request.files.get('uploaded_file')
df = pd.read_csv(uploaded_file)
You can also use io.BytesIO()
to create file-like
object in memory.
from PIL import Image
import io
uploaded_file = request.files.get('uploaded_file')
data = uploaded_file.read()
file_object = io.BytesIO(data)
#file_object.seek(0)
img = Image.open(file_object)
img.save('new_name.jpg')
import pandas as pd
import io
uploaded_file = request.files.get('uploaded_file')
data = uploaded_file.read()
file_object = io.BytesIO(data)
#file_object.seek(0)
df = pd.read_csv(file_object)
But this is more useful when you want to save/generate data without writing on disk and later send it back to browser.
uploaded_file = request.files.get('uploaded_file')
img = Image.open(uploaded_file)
# generate smaller version
img.thumbnail((200,200))
# write in file-like object as JPG
file_object = io.BytesIO()
img.save(file_object, 'JPEG')
# get data from file
data = file_object.getvalue() # get data directly
# OR
#file_object.seek(0) # move to the beginning of file after previous writing/reading
#data = file_object.read() # read like from normal file
# send to browser as HTML
data = base64.b64encode(data).decode()
html = f'<img src="data:image/jpg;base64, {data}">'
Python flask upload file but do not save and use
I know that this is very outdated but for the sake of people landing here for similar inquiry, here it is if you want to save AND read your file after wards. Seems like Werkzeug's FileStorage class (which is the class to handle uploaded file in Flask) pointing to end of file after every action (saving or reading). So we have to move the pointer up to the beginning of the file before doing any subsequent action. I am using python's pandas in my answer below because I usually read csv into dataframe.
import pandas as pd
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
## snippet to read code below
file.stream.seek(0) # seek to the beginning of file
myfile = file.file # will point to tempfile itself
dataframe = pd.read_csv(myfile)
## end snippet
return "yatta"
else:
return "file not allowed"
return render_template("index.html")
Flask upload file without saving it
This simplified code works in my app.
import tempfile
tempdirectory = tempfile.gettempdir()
class UploadView(Roled, BaseView):
if request.method == 'POST':
if request.form['action'] == 'Upload':
file = request.files['newfile']
filename = secure_filename(file.filename)
file.save(os.path.join(tempdirectory, filename))
and in the template:
<form action="" method="POST" enctype="multipart/form-data">
<div>
<input type="file" name="file"/>
</div>
<input class="btn" type="submit" name="action" value="Upload">
</form>
How to parse csv into dictionary in Python without saving file?
Your error says your file is encoded, decode it.
fstring = f.read().decode("utf8")
Opening a file that has been uploaded in Flask
Answering my own question in case someone else needs this.
FileStorage objects have a .stream attribute which will be an io.BytesIO
f = request.files['data_file']
df = pandas.read_csv(f.stream)
Return File to client side without save in sever side which download from GCP cloud storage
Base on JohnHanley's comments able to achieve the goal with the below code. from that way can serve any files without concern about the content type
@app.route('/api/download-file', methods=['GET'])
@token_required
def download_blob():
"""Downloads a blob."""
file_name = "dir/" + request.args.get('file_name')
storage_client = storage.Client()
bucket = storage_client.bucket(app.config.get('CLOUD_STORAGE_BUCKET'))
blob = bucket.get_blob(file_name)
content_type = None
try:
content_type = blob.content_type
except:
pass
file = blob.download_as_string()
print(type(file), "downloaded type")
return Response(file, mimetype=content_type)
Related Topics
List on Python Appending Always the Same Value
Cartesian Product of Two Lists in Python
Execute Multiple Commands in Paramiko So That Commands Are Affected by Their Predecessors
Making the Background Move Sideways in Pygame
Using Django Time/Date Widgets in Custom Form
Applying Function with Multiple Arguments to Create a New Pandas Column
Multiple Modeladmins/Views for Same Model in Django Admin
How to Make a Scatter Plot Colored by Density in Matplotlib
What Are the Differences Between JSON and Simplejson Python Modules
Pandas: Merge (Join) Two Data Frames on Multiple Columns
Converting Dict to Ordereddict
Combine Pool.Map with Shared Memory Array in Python Multiprocessing
Using Python Iterparse for Large Xml Files
How to Obtain the Element-Wise Logical Not of a Pandas Series