Downloaded filename with Google App Engine Blobstore

后端 未结 4 2049
余生分开走
余生分开走 2021-02-14 10:52

I\'m using the Google App Engine Blobstore to store a range of file types (PDF, XLS, etc) and am trying to find a mechanism by which the original filename of the uploaded file -

4条回答
  •  面向向阳花
    2021-02-14 11:08

    There is an optional 'save_as' parameter in the send_blob function. By default this is set to False. Setting it to True will cause the file to be treated as an attachment (ie it will trigger a 'Save/Open' download dialog) and the user will see the proper filename.

    Example:

    class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
        def get(self, resource):
            resource = str(urllib.unquote(resource))
            blob_info = blobstore.BlobInfo.get(resource)
            self.send_blob(blob_info,save_as=True)
    

    It is also possible to overwrite the filename by passing in a string:

    self.send_blob(blob_info,save_as='my_file.txt')
    

    If you want some content (such as pdfs) to open rather than save you could use the content_type to determine the behavior:

    blob_info = blobstore.BlobInfo.get(resource)
    type = blob_info.content_type
    if type == 'application/pdf':       
        self.response.headers['Content-Type'] = type
        self.send_blob(blob_info,save_as=False)
    else:
        self.send_blob(blob_info,save_as=True)
    

提交回复
热议问题