JR Galia
JR Galia

Reputation: 17269

Download Blob from BlobStore in AppEngine with Filename

Files are stored in Appengine Blobstore. I have a servlet handler to allow user to download the file. I used BlobStoreService to do this.

In my servlet, I have the following:

BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
BlobKey blobKey = new BlobKey( "SOME_BLOB_KEY_HERE" );
blobstoreService.serve(blobKey, res);

It works perfectly. The file name of the downloaded file came from the servlet mapping.

How to set the file name of the file in my servlet?

Upvotes: 1

Views: 1407

Answers (1)

Eddie Lau
Eddie Lau

Reputation: 668

You need to add a "Content-Disposition" header to the response:

BlobstoreServiceFactory.getBlobstoreService().serve(blobKey, resp);
BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey);

String encodedFilename = URLEncoder.encode(blobInfo.getFilename(), "utf-8");
encodedFilename.replaceAll("\\+", "%20");
resp.setContentType("application/octet-stream");

resp.addHeader("Content-Disposition", "attachment; filename*=utf-8''" + encodedFilename );

Upvotes: 6

Related Questions