1

My client side code deals with canvas drawing, and i generate a blob zip file of the shapes using javascript. I want to use the zip at the server side. However, I am not able to find any solution.

I have tried using remote command, but using that i can only pass string parameters to the JSF bean. I tried hacking the primefaces fileUpload plugin somehow so that I could pass my blob to the backing bean, but that also didn't work out.

Ashish Mathew
  • 783
  • 5
  • 20

1 Answers1

4

You can pass blob from JavaScript to JSF bean by encoding blob to some string representation, for example, to Base64 and passing it to Primefaces p:remoteCommand, sending it to managed bean and finally decoding it there back to byte array.

Procedure would be following:

  1. Convert blob to Base64

     function prepareAndSendBlobToManagedBean(){
                //zipped file as blob
                var blob=...;
                ///convert blob to Base64 string
                var blobBase64String=convertToBase64(blob);
                sendBlobBase64([{name : 'blobBase64Name', value : blobBase64String}]);
            }
    

(for converting from blob to base64 reffer to this accepted answer)

  1. Add p:remoteCommand to your page

    <p:remoteCommand name="sendBlobBase64" 
                     actionListener="#{yourBean.onBlobBase64Sent}" process="@this"/>
    
  2. In managed bean named yourBean you can catch it with

    public void onBlobBase64Sent() {
       String blobBase64 = (String)FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("blobBase64Name");
       byte[]blob=java.util.Base64.getDecoder().decode(blobBase64);
    }
    
Dusan Kovacevic
  • 1,377
  • 1
  • 13
  • 19
  • Thanks for your help. I was able to do using base64. But I am now running into problems(je when the blob is large(java heap space). Is there a mechanism, where I could use byte stream, instead of String from RequestParameterMap – Ashish Mathew Nov 16 '17 at 15:03
  • @AshishMathew, I suppose that, in that case, you will need to implement some kind of 'chunk-by-chunk' file upload procedure. I suggest that you create another question about that or search SO for ideas. – Dusan Kovacevic Nov 17 '17 at 11:24