Reputation: 1374
I am trying to upload a image and then redirect to another page when submit button is hit. But when i use back button in the browser and again upload the image, i get a 404 Not Found The resource could not be found. No such upload session
I have searched everywhere to resolve this issue but could not find any working any solution. Here is my code:
class PhotoUploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
try:
upload = self.get_uploads()[0]
user_photo = Image(blob_key=upload.key())
user_photo.put()
self.redirect('/view_photo/%s' % upload.key())
except:
self.error(500)
class ViewPhotoHandler(Handler, blobstore_handlers.BlobstoreDownloadHandler):
def get(self, photo_key):
if not blobstore.get(photo_key):
self.error(404)
else:
image = images.get_serving_url(photo_key, size=None, crop=False, secure_url=None, filename=None, rpc=None)
self.render('edit.html', image_key=image)
class MainHandler(Handler):
def get(self):
upload_url = blobstore.create_upload_url('/upload_photo')
params = {'upload_url': upload_url,}
self.render('imagify.html', **params)
The only way i could upload a image after going back is by reloading that page. What is the method of doing this without manually reloading the page? After looking everywhere it looks like it is a issue with blobstore. Please help.
Upvotes: 0
Views: 637
Reputation: 1020
Use Jquery to refresh a page.
Force reload/refresh when pressing the back button
Store data from the fields temporarily and fill after the reload.
How to get JS variable to retain value after page refresh?
Upvotes: 0
Reputation: 1374
I added these couple of lines to set headers in the MainHandler
self.response.headers.add_header("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0")
self.response.headers.add_header("Expires","0")
I am no longer getting the error, but this only solves the problem partly. If i go back using the back button a new session is started, so user will have to fill the form again. But it is not possible to keep the data when the user go back and then submit the form in the same session with the same data. This solution will do for now.
Upvotes: 0
Reputation: 39814
The upload URL is only valid once and becomes invalid after the first upload is executed. The uniqueness of the URL is what the blobstore relies upon to generate a unique blob ID to each uploaded file.
When you use the back button in the browser and click on the cached form's submit button you're actually re-using the previous upload session's URL, which is why you're getting the error.
When you reload the page the form is regenerated and contains a freshly generated upload URL, which works for the next upload.
Upvotes: 2