How do I access an uploaded file with Bottle? - python

I'd like to upload a text file with bottle, and then read it. How can I do this? Below is what I've tried and didn't work.
HTML:
<form action="/load_from_file" method="post">
<div>
Load File: <input type="file" name="file"/>
<input type="submit" value="Submit"/>
</div>
</form>
Bottle route:
#post('/load_from_file')
def load_from_file():
list_file = request.forms.get("file")
print request.files.get("file")
print request.files["file"]
return "how do I get the uploaded file?"

You should add attribute enctype="multipart/form-data" to form tag.

Related

pyFlask is putting my inputs in my url browser

Briefly, python Flask is the workbench of web hosting I use, and I am trying to create an input form that doesn't appear in your history.
This is my form html:
<form name="ViewWindow" action="/home/ViewWindow/ViewWindowResult/">
<input name="url" type="url" required="required" placeholder="URL Here">
<input type="submit" value="Go">
</form>
And this is the python code working with the input url:
#web_site.route('/home/ViewWindow/ViewWindowResult/', methods=('GET', 'POST'))
def ViewWindowResult():
urlboi = request.values.get('url')
response = urllibrequest.urlopen(url) # import urllib.request as urllibrequest
htmlBytes = response.read()
htmlstr = htmlBytes.decode("utf8")
return html("ViewWindowResult.html", value=htmlstr)
My goal is to get here; /home/ViewWindow/ViewWindow/ViewWindowResult/,
but I end up getting here when I input "https://www.w3schools.com/tags/"; /home/ViewWindow/ViewWindowResult/?url=https%3A%2F%2Fwww.w3schools.com%2Ftags%2F
Why does Flask put my inputs in the url string? I do not intend to do this anywhere.
Edit: You can check this out by going to https://sm--supermechm500.repl.co/home/ViewWindow/
Try specifying the form method like so:
<form name="ViewWindow" action="/home/ViewWindow/ViewWindowResult/" method="post">
<input name="url" type="url" required="required" placeholder="URL Here">
<input type="submit" value="Go">
</form>
use post method like
<form name="ViewWindow" action="/home/ViewWindow/ViewWindowResult/" method="post">
<input name="url" type="url" required="required" placeholder="URL Here">
<input type="submit" value="Go">
</form
and then you python code is
#web_site.route('/home/ViewWindow/ViewWindowResult/', methods=('GET', 'POST'))
def ViewWindowResult():
input=request.form['url']
#write your code here
return(input)
its working for me it will print the url which same you entered

Bottle request.files.getall() returns empty list

I got problem with batch image upload using bottle. The request.files.getall() returns empty list, even though I am selecting and uploading files.
My form looks as follows:
<form action="/upload" method="POST">
<div class="form-group">
<label for="gallery">Select images:</label>
<input id="gallery" type="file" name="gallery" accept=".gif,.jpg,.jpeg,.png" multiple>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
My controller looks like this:
#route('/upload', method='POST')
def newGallery():
name = request.forms.get('name')
pictures = request.files.getall('gallery')
for picture in pictures:
print(picture.filename)
return template('new.html')
Thanks for help.
forms.getall(*) worked
*.py file
#bottle.get('/go')
def go():
return bottle.template('new.html')
#bottle.post('/go')
def goo():
name = bottle.request.forms.get('email')
pictures = bottle.request.forms.getall('gallery')
for picture in pictures:
print(picture)
return bottle.template(' hi {{name}}, {{picture}} ', name=name, picture=picture)
Also, changed form tag in new.html to
<form method="post">

Uploading Images Django Python

html:
<label for="image">Image</label>
Select a file: <input type="file" name="img">
Python:
image = (request.FILES.get("img"))
When I print image it prints none. If I just use {{form}} in between the <form> </form> tags it does send the image across to the view. However, for some specific reason I do not want to use the {{form}} method.
Any ideas how I can approach this problem? Thanks in advance!
Make sure your <form> has enctype="multipart/form-data", so it should look something like:
<form method="post" action="{URL}" enctype="multipart/form-data">
<input ...>
</form>

Python GAE Problems with combining requestHandler and BlobstoreUploadHandler

<form action="/fileupload" enctype="multipart/form-data" method="post">
<label>Title<input name="name"></label>
<label>Author<input name="author"></label>
<label>Year <input name="year"></label>
<label>Link <input name="link"></label>
<input type="file" name="file">
<input type="submit">
</form>
The question is why self.get_uploads() returns nothing,when self.request.get('file') works (or I suggest it works)
class FileUploadHandler(H.Handler,blobstore_handlers.BlobstoreUploadHandler):
def post(self):
name=self.request.get('name')
if name:
logging.error(name)
author=self.request.get('author')
if author:
logging.error(author)
year =self.request.get('year')
if year and year.isdigit():
year=int(year)
f =self.get_uploads('file')#FILE NOT FOUND
#f=self.request.get('file') # WORKS FINE
if not f:
logging.error("FILE NOT FOUND")
self.redirect("/files")
I looked at the sample app but they also use self.request.get('file')
The answer is that you should create upload url for blob.
You need get_uploads()[0] or name.
Simple example on the uploads handler (Compare and make appropriate changes in your code):
<form name="myform" action="{{ upload_url }}" method="post" enctype="multipart/form-data">
<h1>Select an Image</h1>
<input name="file" type="file"><br>
<input type="submit" value="Upload">
</form>
Handler:
class UploadBlobsHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
try:
upload = self.get_uploads()[0]
logging.info(upload)
url = images.get_serving_url(upload)
# Do something with it.
except:
self.redirect('/uploadform/?error', abort=True)
self.redirect('/uploadform/?success&image_url=' + url)
Example on how to use uploads with blobstorrhandlers: gae-image-upload-example
This is a full solution to my problem:
<form action="{{upload_url}}" enctype="multipart/form-data" method="post">
<label>Title<input name="name"></label>
<label>Author<input name="author"></label>
<label>Year <input name="year"></label>
<label>Link <input name="link"></label>
<input type="file" name="file">
<input type="submit">
</form>
where upload_url =blobstore.create_upload_url('/fileupload',max_bytes_per_blob=10*1000*1000)
and the code to process the request is next:
upload=self.get_uploads('file')[0]

Pyramid - uploading images and storing on disk

I'm trying to implement a system of uploading a file (image) to the server running pyramid. Right now, this code gives me an AttributeError: 'unicode' object has no attribute 'file' exception:
Server-side:
session = Session()
username = authenticated_userid(request)
if username == None:
return HTTPNotFound()
else:
user = session.query(User).filter(User.username == username).first()
if 'photo.submitted' in request.params:
input_file = request.POST['file_input'].file
tmp = '../static/images/%s' % (session.query(ProfilePic).order_by(-ProfilePic.photo_id).first().photo_id + 1)
open(tmp, 'w').write(input_file.read())
tmp.close()
return Response('OK')
return {}
HTML:
<html>
<body>
<form action="/settings" method="post">
<input type="file" name="file_input" value="Choose image" />
<p><input type="submit" name="photo.submitted" value="Save" /></p>
</form>
</body>
</html>
Something that seems simple, but won't work. I was trying to follow this tutorial, but it seems it only works with video/audio files. How could I make this work?
For file uploads, you need to alter the form enctype to use multipart/form-data:
<html>
<body>
<form action="/settings" method="post" enctype="multipart/form-data">
<input type="file" name="file_input" value="Choose image" />
<p><input type="submit" name="photo.submitted" value="Save" /></p>
</form>
</body>
</html>

Categories