Jagdish
Jagdish

Reputation: 251

GAE/J - Blobstore - how to determine if file is not uploaded

I am working on web application and using GAE/J blobstore tutorial http://code.google.com/appengine/docs/java/blobstore/overview.html I was able to upload file to blobstore.

My Problem is my "upload file" option is OPTIONAL on form. So user may or maynot choose to upload the file on my form. So since this field is optional, I do not have any upfront form validation for this field, but then when i submit the form "a blank document with 0kb file gets uploaded to blobstore" since i am not able to determine if user has selected any file or not inside servlet.

I tried Apache file upload (ServletFileUpload..etc) but it keeps returning null everytime.

so not sure, how do i determine if user have selected any file to upload inside servlet?

            Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
            if (blobs != null && blobs.size() > 0) {
                BlobKey blobkey = blobs.get("myFile");
                blobkeyStr = blobkey.getKeyString();
            }

Upvotes: 3

Views: 1632

Answers (4)

Ashley Schroder
Ashley Schroder

Reputation: 3886

In the dev environment if the user submits a form with an empty file upload, the blobkey will be null, but in production it will be non-null and the blob will be empty. So you should check for both scenarios.

Upvotes: 5

Jason
Jason

Reputation: 181

You can test if a blob was uploaded by checking the size of the blob. If the size is zero, you should delete the blob.

BlobstoreService bs = BlobstoreServiceFactory.getBlobstoreService();
BlobKey blobKey = bs.getUploads(req).get("blob").get(0);
final BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey);
long size = blobInfo.getSize();
if(size > 0){
  //process blob
}else{
  bs.delete(blobKey);
}

Upvotes: 8

DavidB
DavidB

Reputation: 2234

I'm assuming that you are using a form to submit directly to your upload URL? If so, you might want to add validation code on your form itself. If they've selected the form then do an async request to get an upload url to submit to. If there is no form attached then submit to a different URL that doesn't process the blob.

So for instance, when they submit, if the form is attached, submit to your servlet that generates the upload URL like this:

    BlobstoreService service = BlobstoreServiceFactory
            .getBlobstoreService();
    String url = service
            .createUploadUrl("/uploadurl");

    return url;

Upvotes: 1

csturtz
csturtz

Reputation: 6580

FYI it may be more helpful for you to show your code.

Basically, even though your file upload is optional, you still need to send the request from the form submission through the blobstore upload url anyway. If a file was uploaded, your upload handler that gets control from GAE will be able to get a list (map) of all blobs. If no file was uploaded, that list will be empty. From there, you can process the rest of the form submission as you choose.

For the specifics of how to get that list of uploaded blobs, see this section of the documentation, but basically you're going to make this call:

Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);

If that map is empty, there were no blobs uploaded.

Upvotes: 1

Related Questions