-
FEATURED COMPONENTS
First time here? Check out the FAQ!
<button label="Attach">
<attribute name="onClick">
{
org.zkoss.util.media.Media[] media = Fileupload.get(-1);
if (media != null)
{
for (int i = 0; i < media.length; i++)
{
if (media instanceof org.zkoss.image.Image)
{
org.zkoss.zul.Image image = new org.zkoss.zul.Image();
image.setContent(media);
image.setParent(pics);
}
else
{
Messagebox.show("Not an image: "+media, "Error", Messagebox.OK, Messagebox.ERROR);
break;
}
}
}
}
</attribute>
</button>
above is my code to upload the file but I am not being able to upload the fil...because the media is null. I have tried both enabling and disabling the event thread but both ways doesn't work. please help me.
I tried that ....the media array gets element means if I select the file array media legth is showing 1 but as soon as code encounters media[0].getName it is throwing nullpointer exception ...wht could br the prob??
You could try something like this:
public static MyImage uploadImage(Media[] media) { MyImage image = null; try { if (media != null) { for (int i = 0; i < media.length; i++) { if (media<i > instanceof org.zkoss.image.Image) { InputStream uploadedFile = media<i >.getStreamData(); String name = media<i >.getName(); String contentType = media<i >.getContentType(); image = new MyImage(...); } else { Utils.showError("Not an image: " + media<i >); } } } else { Utils.showError("Upload was not completed because no file was selected."); } } catch (Exception e) { Utils.showError("ERROR " + e.getMessage()); } return image; }
and
Toolbarbutton imgUplTbb = new Toolbarbutton("Upload image"); imgUplTbb.setUpload("true"); imgUplTbb.addEventListener("onUpload", new EventListener() { public void onEvent(Event event) throws Exception { UploadEvent e = (UploadEvent) event; MyImage image = Utils.uploadUserImage(e.getMedias()); ..... } } });
Media[]
media<i >
I tried above code but now it is giving error on media.getStreamData as
java.lang.NullPointerException "Attempt to invoke method getStreamData on null value"
why is it like this media is not null as it goes inside the if but media [ i ] is nulll
I also tried media [ i ] .getName() but it gives the same error
Please refer to this guide.
Maybe this thread might help you, too http://www.zkoss.org/forum/listComment/17391-fileupload-from-macrocomponet-argument-type-mismatch
Asked: 2011-08-23 01:08:21 +0800
Seen: 607 times
Last updated: Sep 15 '11