I am learning how to work with file uploads in flask. Earlier from here, i worked with pdf file uploads and read the contents of it. This happens inside client.py file.
Now i would like to pass my file from client to server that is running locally. When i use request.file, it will get it as FileStorage Object. So, without saving or providing file path, i want to upload file from client and pass it to the server to do further process.
class mainSessRunning():
def proces(input):
...
...
return result
run = mainSessRunning()
@app.route('/input', methods=['POST'])
def input():
input_file = request.files['file']
...(extract file from filestorage object "input_file")...
result = run.process(file) ## process is user defined function
return (result)
here i want to send the incoming file through process()
function to server running locally. How do i do this? I came across same question but couldn't able to find anything
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 os
import json
from flask import Flask, render_template, url_for, request, redirect, jsonify
from PIL import Image
Upload = '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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With