HTML/Python Upload and download a file - python

I have a button type ="file", which uploads a file. I'm using Google App Engine.
<form action="/sign?%s" method="post">
<div><br><br>Select a file: <input type="file" value="file"></div>
<form method="get" action="file">
<button type="submit">Download</button>
</form>
I have two questions:
Is it possible, after choosing a file to display more options (rather than just the file name), for example format and timestamp?
I would like to create a download button now, that downloads the file I have uploaded using the above upload button, is there a simple way to do that?

Related

How to Execute Python Code on Server from an HTML Form Button

I want to execute python code when user clicks an HTML Form Button. How is it going to be possible? Is it possible that users are not able to view the python code on the server? The forms input are going to be variables in the python code. I am using Flask framework and python 2.7. Security is not a concern yet.
my route.py file:
from pytube import YouTube
from moviepy.editor import *
import os
app = Flask(__name__)
#app.route("/",methods=['GET','POST'])
def landing():
return render_template("index.html",ytdir=ytdir,ytlink=ytlink)
if __name__ == "__main__":
app.run(debug=True)
The index.html in the Template folder:
<form method="POST" action="/">
<input type="text" class="form-control" name="ytdir" placeholder="Example: C:\Downloads">
<h4>The YouTube Video that you want to download</h4>
<textarea class="form-control" rows="3" name="ytlink" placeholder="Example: https://www.youtube.com/watch?v=HipaBLsGB_Y"></textarea>
</form>
<input class="btn btn-primary" type="submit" value="Download">
basically, I have a python code that download the YouTube video into the local drive by using ytdir and ytlink.
Where should I place the python code so that it is executed when the button is clicked.
I recommend for you to do an "api call"
#app.route("/ytdl", methods=["GET"])
def download_video(link):
# call your download code here
# and return the video as an input stream
# read the flask tutorial on how to do that
In the javascript use axios or something to download the file using jquery
like $button.click(() => axios.get(http://<localhost url>:<port>/ytdl) go and read the doc again how to do ajax call

python flask iframe and download

I have to perform two actions based on radio button selection, either download or view a document
<form method="post" action="{{ url_for('page_after_submit') }}">
<p> Your resume </p>
<div class="radio">
<label> <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked> Download document </label>
</div>
<div class="radio">
<label> <input type="radio" name="optionsRadios" id="optionsRadios2" value="option2"> View document </label>
</div>
<input type="submit" />
</form>
My page_after_submit has this code...
#app.route(local.URL_PREFIX + '/page_after_submit/', methods=['POST'])
def after_submit():
if 'option1' == request.form['optionsRadios']:
return/redirect ("download from this url")
if 'option2' == request.form['optionsRadios']:
return/redirect ("view in this iframe")
return (Url_for('go back to submit page if you are here')
I know my form can only have one action which is '/page_after_submit/', what code (HTML or Python in flask) I need to complete rest of my actions ??? I tried to put the iframe tags with complete download file address in the redirect for option2 but doesn't work. I also want this iframe to pop up not open a new browser window. Plus for the download, don't know what to do specially different operating system may have different path for download directory.
My goal is to not have any javascript as well, don't know if it's possible or not. Thanks in advance.
You need to craft a different response depending on how they want to see the data.
Download File
If they choose option 1, you need to set the headers and response to allow the browser to trigger a file download. Here's how you can do something like that.
View File in Iframe
If they choose option 2, you need to return an HTML response which loads the file. This can be done in an Iframe if you'd like, but it's not necessary. Here's one possible way to do that, but many others exist.

Upload .xls file with html input file

I can successfully upload a .csv, and a .json file with the following code. However, when I try to upload a .xls files, the app gets locked up. What do I need to do to make this work correctly for Excel files?
class CreateTeam(BaseHandler):
def post(self):
file = self.request.get('file') # this line locks up with Excel files
My html looks like this...
<form method="POST" action="/create_team" enctype="multipart/form-data">
<div>
<p>Upload the file</p>
<input type="file" name="file">
</div>
</form>
I also have some other input fields on the form that are posted with the same submit button. Is this a MIME type issue. If so, how do I set the MIME type for just the uploaded file?

uploading html files and using python

For my forum, I tried uploading a file with html using:
<form action="profiles.py" method="post">
<label class="labelOne" for="pics">Change your Profile pic </label>
<input type="file" name="uploadField" />
My python function takes that file, creates a file under userprofiles, and writes the data to it:
file = open('../data/accounts/userprofiles/'+str(form['name'].value)+'/'+'profilepics', 'w+')
file.write(str(form.getvalue('uploadField')))
file.close()
So, If I want burger.jpg to be my picture, I upload it, python takes that and creates a file with that name burger.jpg using w+ and then it writes the data to it(which should be the image).
However for some reason, I get no image.
What should I try next?
Set the enctype of your form to multipart/form-data
<form action="profiles.py" method="post" enctype="multipart/form-data">

Creating an object with an external form

I have a upload form that is going to S3 --
<form action="https://test.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="id_file" />
<input type="submit" value="Upload to Amazon S3" name="upload">
</form>
When the form is submitted to S3, I also need to create an object in my db and get the id of the object. How would I do this (without redirecting to my view)? Thank you.
You could use an iframe to send the form and js to search for the id you are looking for in the retrieved page.
You could also use another script to perform the post method like php with the curl library. You could grab all the data retrieved from there, search for what you are looking for and add what you need to your database.

Categories