Question

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

Was it helpful?

Solution

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

OTHER TIPS

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]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top