Ankur Jain
Ankur Jain

Reputation: 1404

Serve File from GAE Blobstore

I have faced this problem several times that whenever we serve files from Blobstore API. It is always downloaded as :

serve.fileNameExtention

is there any possible way that we can serve or download the file with the name we saved it.

i.e. someName.docx

If it is possible please tell me how to do it, because I have searched over internet but I couldn't found out any solution for it.

Upvotes: 0

Views: 1194

Answers (3)

sopheamak
sopheamak

Reputation: 393

BlobKey blobKey = new BlobKey(key);                

BlobInfo blobInfo =  new BlobInfoFactory().loadBlobInfo(blobKey);
// set response header
response.setContentType(blobInfo.getContentType());
response.setHeader("Content-Disposition", "filename=" + blobInfo.getFilename());

// serve blob
blobService.serve(blobKey, response);

Upvotes: 1

Igor Artamonov
Igor Artamonov

Reputation: 35961

Set it in header:

public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws IOException {
        BlobKey blobKey = new BlobKey(req.getParameter("blob-key")); //example
        String filename = "someName.docx";
        res.setHeader("Content-Disposition", "attachment; filename=\"" +fileName +\"");

        blobstoreService.serve(blobKey, res);
    }

Upvotes: 3

Rick Mangi
Rick Mangi

Reputation: 3769

You would have to change the file name in the url that you are serving the file from. You can use something like urlRewriteFilter (https://code.google.com/p/urlrewritefilter/) to rewrite the urls to allow you to still use the "serve" servlet, but change the urls on their way out of the servlet to whatever you want them to be.

Upvotes: 0

Related Questions