如何将在javascript中创建的Blob传递给JSF Bean [英] How to pass a blob created in javascript to JSF bean
问题描述
我的客户端代码处理画布绘图,并且我使用JavaScript生成了形状的Blob zip文件. 我想在服务器端使用zip.但是,我找不到任何解决方案.
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.
我尝试使用远程命令,但是使用该命令我只能将字符串参数传递给JSF bean. 我试图以某种方式破解primefaces fileUpload插件,以便可以将我的Blob传递给支持bean,但这也没有解决.
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.
推荐答案
您可以通过将blob编码为某种字符串表示形式(例如,传递到 Base64 并将其传递给)来将blob从JavaScript传递到JSF bean. Primefaces p:remoteCommand
,将其发送到托管bean,最后将其解码回字节数组.
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.
操作步骤如下:
-
将blob转换为Base64
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}]);
}
(用于从blob转换为base64,请参考此已接受的 answer )
(for converting from blob to base64 reffer to this accepted answer)
-
在页面中添加p:remoteCommand
Add p:remoteCommand to your page
<p:remoteCommand name="sendBlobBase64"
actionListener="#{yourBean.onBlobBase64Sent}" process="@this"/>
在名为 yourBean 的托管bean中,您可以使用
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);
}
这篇关于如何将在javascript中创建的Blob传递给JSF Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!