0

File upload problem :want to store image browse by client to be store on server

asked 2010-03-22 04:29:08 +0800

techvts gravatar image techvts
120 2 5

updated 2010-03-22 04:29:47 +0800

<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c" ?>
<?evaluator name="mvel"?>

<zk>
<window id="prodwin" title="UPLOAD" width="100%" height="100%" border="normal" use="zkadsmine.ui.ProdinfoWindow">

<button label="Upload" onClick="prodwin.upload()" />
<vbox id="pics" />
</window>
</zk>


upload method in prodinfowindow

public void upload() {
System.out.println("In upload");
try {
System.out.println("before media");
org.zkoss.util.media.Media media = org.zkoss.zhtml.Fileupload.get();

System.out.println("media:"+media);
if (media != null) {
System.out.println("In media");
File f=new File(media.getName());
System.out.println(" media name:"+media.getName());
InputStream inputStream= media.getStreamData();
OutputStream out=new FileOutputStream(f);
byte buf[]=new byte[1024];

int len;
while((len=inputStream.read(buf))>0)
out.write(buf,0,len);
out.close();
inputStream.close();
String filepath = f.getAbsolutePath();
Messagebox.show(filepath);


}
else
{
System.out.println("media null");
}

} catch (Exception e) {
showError("ERROR " + e.getMessage());
}

}

in above code
org.zkoss.util.media.Media media = org.zkoss.zhtml.Fileupload.get();
here value of media comes null.
i couldn't get desire output

plz help if anybody knows how to store image from client to server

delete flag offensive retag edit

17 Replies

Sort by ยป oldest newest

answered 2012-08-13 13:06:19 +0800

Webtibet3 gravatar image Webtibet3
30

hello,

i have the code with index.zul and upload.ctrl but i have two warnings "public class UploadCtrl extends GenericForwardComposer" = "GenericForwardComposer is a raw type. References to generic type GenericForwardComposer<T> should be parameterized"

and : "super.doAfterCompose(comp);" = "Type safety: The method doAfterCompose(Component) belongs to the raw type GenericForwardComposer. References to generic type GenericForwardComposer<T> should be parameterized"


thank's for your help i really need

link publish delete flag offensive edit

answered 2010-12-29 01:07:50 +0800

yaryan997 gravatar image yaryan997
210 2

@techvts

Hi.. friend

your code for uploading image file works very well in my project development.
So i am very thankful to you..

keep posting this kind of articles.
thanx

link publish delete flag offensive edit

answered 2010-10-14 04:35:32 +0800

ssaravanan gravatar image ssaravanan
18

hai,

pls tell me the way to upload swf file and use it to show from its directory

link publish delete flag offensive edit

answered 2010-10-11 07:47:00 +0800

Bobzk gravatar image Bobzk
444 1 8

Not sure if it helps, but a ZK 5 version in Python looks like -

    
   def onUpload_upload_button(self, event):
        real_event = Events.getRealOrigin(event)
        media = real_event.getMedia()
        self.filename.setValue(media.getName())
        if media.getFormat().upper() != 'PDF':
            errmsg = "File is not a PDF - looks like %s" % media.getFormat()
            Messagebox.show(errmsg, "Warning", Messagebox.OK, Messagebox.EXCLAMATION)
            return()

        if media.inMemory():
            buf = ByteBuffer.wrap(media.getByteData())
        else:
            med = media.getStreamData()
            fc = med.getChannel()
            buf = fc.map(MapMode.READ_ONLY, 0, fc.size())

You need the following in your ZUL -

                                <button id="upload_button" label="Upload" upload="true"
                                    mold="trendy" />

The above works and in this case checks for PDF but can easilly be changed to check for SWF or JPG etc.

link publish delete flag offensive edit

answered 2010-04-05 19:53:47 +0800

ashishd gravatar image ashishd flag of Taiwan
1972 6

Hi techvts,
I used your code with zk 3.6.3 without a single modification and it worked. You could download my test project from here and see if it works in your development environment.
Let me know asap after you download so that I can remove it. It is uploaded to a temp location.

Thanks
- Ashish

link publish delete flag offensive edit

answered 2010-04-03 00:40:00 +0800

techvts gravatar image techvts
120 2 5

hi Ashish ,
i am using zk 3.6.3
i am getting media =null always so my code doesn't work.
r u getting value of uploaded file.

thanks

link publish delete flag offensive edit

answered 2010-04-02 06:16:09 +0800

ashishd gravatar image ashishd flag of Taiwan
1972 6

Hi techvts,
I have tried your code and it worked on ZK 3.6.3. I will again try it on 3.6.2 but in the mean time just wanted to ask if it is possible for you to upgrade to ZK 3.6.3?

Thanks
- Ashish

link publish delete flag offensive edit

answered 2010-04-02 02:03:06 +0800

techvts gravatar image techvts
120 2 5

This is my total code i don't get desire result plz help

uploadPage.zul
<zk>
<window apply="zkadsmine.ui.UploadCtrl" >
<button id="uploadBtn" label="Upload file" />
<separator />
<image id="img" />
<separator />
<label id="msgLb" />
</window>
</zk>

UploadCtrl .java

package zkadsmine.ui;

import org.zkoss.zk.ui.util.GenericForwardComposer;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.zkoss.util.media.Media;
import org.zkoss.zk.ui.Component;

import org.zkoss.zul.*;

public class UploadCtrl extends GenericForwardComposer {

private Image img;
private Label msgLb;
private static final int FILE_SIZE = 100;// 100k
private static final String SAVE_PATH = "c:\\myfile\\media\\";

@Override
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);
}

public void onClick$uploadBtn() {
msgLb.setValue("");


try {
Media media = Fileupload.get();


if(media == null){
msgLb.setValue("please select a file");
return;
}

String type = media.getContentType().split("/")[0];
if (type.equals("image")) {
if (media.getByteData().length > FILE_SIZE * 1024) {
msgLb.setValue("File size limit " + FILE_SIZE + "k");
return;
}
org.zkoss.image.Image picture = (org.zkoss.image.Image) media;
img.setContent(picture);
}

saveFile(media);

} catch (InterruptedException e) {
e.printStackTrace();
}
}

private void saveFile(Media media) {
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
InputStream fin = media.getStreamData();
in = new BufferedInputStream(fin);

File baseDir = new File(SAVE_PATH);

if (!baseDir.exists()) {
baseDir.mkdirs();
}

File file = new File(SAVE_PATH + media.getName());

OutputStream fout = new FileOutputStream(file);
out = new BufferedOutputStream(fout);
byte buffer[] = new byte[1024];
int ch = in.read(buffer);
while (ch != -1) {
out.write(buffer, 0, ch);
ch = in.read(buffer);
}
msgLb.setValue("sucessed upload :" + media.getName());
} catch (IOException e) {
throw new RuntimeException(e);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (out != null)
out.close();

if (in != null)
in.close();

} catch (IOException e) {
throw new RuntimeException(e);
}
}

}

}

link publish delete flag offensive edit

answered 2010-04-02 01:59:08 +0800

techvts gravatar image techvts
120 2 5

In first post i has given every thing there i get media =null
so my code doesn't work i am using zk 3.6.2 in eclipse

link publish delete flag offensive edit

answered 2010-03-26 03:14:38 +0800

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

Could you please provide us with a runnable example?
And tell us how to reproduce your issue.

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: 2010-03-22 04:29:08 +0800

Seen: 2,223 times

Last updated: Aug 13 '12

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