I use Flask to upload files. To prevent saving the same file twice, I am going to calculate md5 from the contents of the file and save the file as. if the file already exists.
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
img_key = hashlib.md5(file).hexdigest()
Unfortunately, hashlib.md5 throws an exception:
TypeError: must be string or buffer, not FileStorage
I already tried file.stream - same effect.
Is there a way to get md5 from a file without saving it temporarily?
source
share