Python GAE Problems with combining requestHandler and BlobstoreUploadHandler - python

<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]

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">

How do I access an uploaded file with Bottle?

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.

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>

How to pass variables to a form before it is processed

I have the following template form, containing several variables.
<form action="https://me.s3.amazonaws.com/" method="post" enctype='multipart/form-data' class="upload-form">
<input type="hidden" name="key" value="videos/{{filename}}">
<input type="hidden" name="AWSAccessKeyId" value="{{access_key}}">
<input type="hidden" name="acl" value="public-read">
<input type="hidden" name="policy" value="{{policy}}">
<input type="hidden" name="signature" value="{{signature}}">
<input type="hidden" name="Content-Type" value="{{content_type}}">
<input name="file" type="file">
<input type="submit" value="Upload" name="upload">
</form>
However, as soon as the submit button is hit, the form is sent to amazon, and I'm not able to pass it variables. This is what I've been trying to do, unsuccessfully --
if 'upload' in request.POST:
policy = base64.b64encode(...)
signature = base64.b64encode(
hmac.new('secret_key', policy, sha).digest())
file = request.POST['files']
filename=file.name
content_type=mimetypes.guess_type(filename)[0]
What do I need to do to pass the variables to the form after the POST request but BEFORE amazon processes the form? Thank you.
You should change your form's action to your django view and in your view you can re-post to https://me.s3.amazonaws.com/:
In your template
<form action="http://mywebsite/upload" method="post" ...
In your view.py:
def upload(request):
# Your treatment here.
# Post the data to amazon S3.
urllib2.urlopen("https://me.s3.amazonaws.com/", your_data)
...
You could change the form to POST to one of your own views, then do your post-processing in your view, and then within your view code, issue a POST to Amazon with the correct values using, say, urllib2 or similar.

Categories