0

Fileupload.get(-1); is returning null

asked 2011-08-23 01:08:21 +0800

bhushan2169 gravatar image bhushan2169
138

<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.

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2011-08-28 04:01:48 +0800

RichardL gravatar image RichardL
768 4

You could have the button listen to the onUpload event then use the getMedias method to get the Media array from the UploadEvent.

link publish delete flag offensive edit

answered 2011-08-29 03:16:04 +0800

bhushan2169 gravatar image bhushan2169
138

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??

link publish delete flag offensive edit

answered 2011-08-29 06:31:50 +0800

RichardL gravatar image RichardL
768 4

updated 2011-08-29 06:49:33 +0800

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());
                        .....
                        }
                    }
                });

(P.S square brackets are displaying as angle brackets in the code section, i.e.
Media[]
is ok but
media<i >
is not)

link publish delete flag offensive edit

answered 2011-08-30 03:12:32 +0800

bhushan2169 gravatar image bhushan2169
138

updated 2011-08-30 03:13:20 +0800

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

link publish delete flag offensive edit

answered 2011-08-30 03:14:03 +0800

bhushan2169 gravatar image bhushan2169
138

updated 2011-08-30 03:32:12 +0800

I also tried media [ i ] .getName() but it gives the same error

link publish delete flag offensive edit

answered 2011-08-31 20:45:49 +0800

jumperchen gravatar image jumperchen
3909 2 8
http://jumperchen.blogspo... ZK Team

Please refer to this guide.

link publish delete flag offensive edit

answered 2011-09-15 07:47:45 +0800

RichardL gravatar image RichardL
768 4

Maybe this thread might help you, too http://www.zkoss.org/forum/listComment/17391-fileupload-from-macrocomponet-argument-type-mismatch

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-08-23 01:08:21 +0800

Seen: 607 times

Last updated: Sep 15 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More