Extract file from file storage object in flask
What do you mean "extract"? If you want get the bytes of file, you can use content = request.files['file'].read()
.
And then send this content to any where you want: res = requests.post(url, content)
import osimport jsonfrom flask import Flask, render_template, url_for, request, redirect, jsonifyfrom PIL import ImageUpload = 'static/upload'app = Flask(__name__)app.config['uploadFolder'] = Upload@app.route("/")def index(): return render_template("Index.html")@app.route("/upload", methods = ['POST', 'GET'])def upload(): file = request.files['imgfile'] filename = file.filename file.save(os.path.join(app.config['uploadFolder'], file.filename)) print(file.filename) img = Image.open(file) li = [] li = img.size imgobj = { "name" : filename, "width" : li[0], "height" : li[1] } json_data = json.dumps(imgobj) with open('jsonfile.json', 'w') as json_file: json.dump(imgobj, json_file) return render_template('index.html', filename = filename) # comment this line to only get the json data. # return render_template('index.html', json_data = json_data) # uncomment this line to get only json data.if __name__ == "__main__": app.run(debug = True, port = 4455)
if you want to extract the file from the file storage object please follow the below steps:By using the below code you can save the file from file storage object and save it locally
<!DOCTYPE html><html><head> <title>Assignment</title></head><body>{% if filename %} <div> <h1>{{filename}}</h1> <img src="{{ url_for('static', filename='upload/'+filename) }}"> </div>{% endif %} <!-- comment this div to only get the json data.--><form method="post" action="/upload" enctype="multipart/form-data"> <dl> <p> <input type="file" name="imgfile" autocomplete="off" required> </p> </dl> <p> <button type="submit" value="Submit"> Submit</button> </p></form><!-- {% if json_data %}<div> <p> {{ json_data}}</div>{% endif %} --> <!-- uncomment this div to only get the json data.--></body></html>