简体   繁体   中英

Problem serving blobs in GAE

Im currently building an web-app that allows users to upload content via blobstore and to later download it.

However, the servlet that takes care of the download is called BlobServiceServlet and whenever a user downloads a blob, the filename is changed to "BlobServiceServlet" and the extension is also changed sometimes to .bin. Does anyone know how to fix this problem?

Add a "Content-disposition" header to the response.

See http://en.wikipedia.org/wiki/MIME#Content-Disposition for an example.

Eg, in the handler,

self.response.headers['Content-Disposition'] = 'attachment; filename=foo.doc'

Just an additional info. This is the code to let the browser know the file size:

BlobInfoFactory blobInfoFactory = new BlobInfoFactory(DatastoreServiceFactory.getDatastoreService());
BlobInfo blobInfo = blobInfoFactory.loadBlobInfo(blobKey);
resp.setContentLength(new Long(blobInfo.getSize()).intValue());
resp.setHeader("content-type", blobInfo.getContentType());
resp.setHeader("content-disposition", "attachment;filename=" + blobInfo.getFilename());
blobstoreService.serve(blobKey, resp);

Note that if you have files with more than 1MB in size, the file size is not sent to browser. GAE reads the blob 1MB at a time and overwrites the file size header in the response.

I found all this information here: http://www.mail-archive.com/google-appengine@googlegroups.com/msg29314.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM