Can't get other files uploaded [python flask] with html - python

The problem is i am able to upload file which are in the same directory as main.py but I am unable to upload other files which are not in the same folder of main.py, suppose - main.py is in c:\users\prabhat\upload\ and the file i am uploading the file is also in c:\users\prabhat\upload\ directory, but if there is file in c:\desktop\B.xlsx then if i upload, then it shows Error, file not found.
how an i resolve this??
index.html code
<form class="" action="data" method="post">
<input type="file" name="upload-file" value="">
<input type="submit" name="" value="Submit">
</form>
main.py
from flask import Flask, render_template, request
import pandas as pd
app = Flask(__name__)
#app.route('/', methods=['GET', 'POST'])
def index():
return render_template('index.html')
#app.route('/data', methods=['GET', 'POST'])
def data():
if request.method == 'POST':
file = request.form['upload-file']
return '''
successfully Done!
'''
if __name__ == '__main__':
app.run(debug=True)

You don't access files uploaded through flask via request.form but rather through request.files. This should get you a valid file handle:
if request.method == 'POST':
file = request.files['upload-file']
See https://flask.palletsprojects.com/en/2.2.x/patterns/fileuploads/ for more info.

Try file = request.files['upload-file'] instead of request.form, and I guess you want to save it to the server, do it with file.save("path/to/your/folder/filename.extension")

Related

how to use Flask to save both uploaded file and user text input in json

in html, I have
<form method="POST" enctype="multipart/form-data" action="/">
<input type="number" name="speed " id="speed">
<input type="file" id="stl_file">
<input type="submit" value="submit">
</form>
I would like to save the value for speed in json file and save the uploaded stl file as well.
What I did in my app.py is
from flask import Flask, render_template, request,flash, redirect, url_for
from werkzeug.utils import secure_filename
import os
import json
app = Flask(__name__)
ALLOWED_EXTENSIONS = {'stl','STL'}
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
#app.route("/")
def index():
return render_template('main.html')
#app.route('/' , methods=['POST'])
def get_data():
if request.method == 'POST':
results =request.form
file = request.files['stl_file']
with open('file.json','w') as f:
json.dump(results, f)
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
# if user does not select file, browser also
# submit an empty part without 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(filename)
return redirect(url_for('index'))
return render_template('main.html')
if __name__ == '__main__':
app.run()
I can run it and I see my main page. However, when I input a number into speed, upload a file, and click submit, I get
Bad Request
The browser (or proxy) sent a request that this server could not understand.
Any suggestion on that? Thanks in advance.
You haven't defined the name attribute of file.
<input type="file" name="stl_file" id="stl_file">

Uploading and Processing files with Flask Rest API

I want to create a REST API using Flask. I have two files at any given time, I will call my API and send these two files, a python script will run on the API side and send me back a JSON Response. Now this is the overall idea of my application , but I don't know how to do all this. As I do not want to save those two files, just take them as an Input and send the result back, I don't think i will need to save the files. But I do not know how to send files to the API, logic part is ready, I just need to know how to send two files, process them and send back the response in just one API call. Thanks in advance
I have just setup the basic API till now.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'Server works'
if __name__== '__main__':
app.run(debug=True)
Found this on the Flask documentation:
import os
from flask import Flask, flash, request, redirect, url_for
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = '/path/to/the/uploads'
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
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 user does not select file, browser also
# submit an empty part without 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('uploaded_file',
filename=filename))
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
'''
In your API backend, you will receive the file by using file = request.files['file']. The name "file" comes from the name of the input tag in the HTML Form you are using to send the file to your backend.
'''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
'''
In this example, the backend is saving the uploaded files to UPLOAD_FOLDER. This example is also using some exception handling to make sure that the user is uploading the correct type of file types.
[EDIT] I misread your question. If you just want to send back a JSON response instead of JSON containing the files content you would do this:
return jsonify({"response": "success"})
Finally, here's a link to the full Flask documentation

"URL Not found error" when trying to load my flask application in browser window

Below is my code. I want to convert excel file to json via my flask application. After running the code, while trying to load up the flask URL in browser, localhost gives the following error:
404 not found error - The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again
What do I need to do? Below is my application code:
from flask import Flask, request, jsonify
import flask_excel as excel
app=Flask(__name__)
#app.route("/upload", methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
return jsonify({"result": request.get_array(field_name='file')})
return '''
<!doctype html>
<title>Upload an excel file</title>
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file><input type=submit value=Upload>
</form>
'''
#app.route("/export", methods=['GET'])
def export_records():
return excel.make_response_from_array([[1,2], [3, 4]], "csv",
file_name="export_data")
if __name__ == "__main__":
app.run()
Since you've defined your app logic under the route #app.route("/upload", methods=['GET', 'POST']) and you do not have any logic defined under your base address: #app.route("/", methods=['GET', 'POST']), you have to load your application using this as your URL:
http://127.0.0.1:5000/upload
And if you are using any other host address or port number in your flask application, you have to change your URL as:
http://Your_flask_IP_Address:Port_Number/upload
Please do comment if this works. Cheers!

Having trouble sending data from html to python using flask

I have an HTML index page that sends input data to a python script which processes the data and outputs it in a 2nd HTML page. On a local host, the process works fine and the data is displayed as desired. But when I try to host the process online, I get an error saying the URL cannot be found. If it helps, I'm using Heroku.
Apologies in advance for any poor lingo. I only just started learning how to code recently.
1st HTML
<form action = "https://webaddress/result" method = "POST">
<h1> Enter info: </h1>
<input type="text" name="info">
<input type="submit" value="Submit"/>
</form>
Python:
from flask import Flask, render_template, request
from bs4 import BeautifulSoup
import requests
# https://doi.org/10.2118/21513-MS
app = Flask(__name__)
#app.route('/')
def student():
return render_template('Trial.html')
#app.route('/result.html',methods = ['POST', 'GET'])
def result():
return render_template("result.html",result = output)
if __name__ == '__main__':
app.run(debug = True)
The input in the 1st HTML would be sent to the python section to be broken down and rearranged (left out that part so the python code wouldn't be too long) before being output into result.html. This worked on a local host using http://localhost:5000/ and http://localhost:5000/result.
When I run it on Heroku, I get the error message:
Not Found
The requested URL /result was not found on this server.
Update: Problem solved.
Having some issues understanding your results() function. output isn't defined anywhere and type isn't used at all.
I believe your action parameter is incorrect.
Try:<form action="/result" method = "POST">
Here is a working version of what I hacked together for you:
from flask import Flask, render_template, request
app = Flask(__name__)
#app.route('/', methods=['GET'])
def home():
return '''
<form action="/result" method = "POST">
<h1> Enter info: </h1>
<input type="text" name="info">
<input type="submit" value="Submit"/>
</form>
'''
#app.route('/result',methods = ['POST', 'GET'])
def result():
if request.method == 'POST':
type = ''
if 'info' in request.form:
result = request.form['info']
return result
if __name__ == '__main__':
app.run(debug=True, port=8888, host='0.0.0.0')
Your form refers to /result (no .html extension), but your route is for /result.html (with the extension). Try removing the .html from your route:
#app.route('/result', methods=['POST', 'GET'])
def result():
return render_template("result.html", result=output)

Uploading multiple files with Flask

Is there a way to receive multiple uploaded files with Flask? I've tried the following:
<form method="POST" enctype="multipart/form-data" action="/upload">
<input type="file" name="file[]" multiple="">
<input type="submit" value="add">
</form>
And then printed the contents of request.files['file']:
#app.route('/upload', methods=['POST'])
def upload():
if not _upload_dir:
raise ValueError('Uploads are disabled.')
uploaded_file = flask.request.files['file']
print uploaded_file
media.add_for_upload(uploaded_file, _upload_dir)
return flask.redirect(flask.url_for('_main'))
If I upload multiple files, it only prints the first file in the set:
<FileStorage: u'test_file.mp3' ('audio/mp3')>
Is there a way to receive multiple files using Flask's built-in upload handling? Thanks for any help!
You can use method getlist of flask.request.files, for example:
#app.route("/upload", methods=["POST"])
def upload():
uploaded_files = flask.request.files.getlist("file[]")
print uploaded_files
return ""
#app.route('/upload', methods=['GET','POST'])
def upload():
if flask.request.method == "POST":
files = flask.request.files.getlist("file")
for file in files:
file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
It works for me.
for UPLOAD_FOLDER if you need add this just after app = flask.Flask(name)
UPLOAD_FOLDER = 'static/upload'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
Using Flask 1.0.2+:
files = request.files.getlist("images")
Where images is the key of the key/value pair. With the Value being the multiple images.
this is a working solution for flask version '1.0.2':
images = request.files.to_dict() #convert multidict to dict
for image in images: #image will be the key
print(images[image]) #this line will print value for the image key
file_name = images[image].filename
images[image].save(some_destination)
basically, images[image] has an image file with save function added to it
Now do whatever you like to do with the data.

Categories

Resources