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 2010-03-22 05:12:09 +0800

rotkad gravatar image rotkad
18

Hello,
I have the same problem:
it worked fine in zk 3.6.3, now I migrated to 5.0.1 and media comes null.
Media media = Fileupload.get();
InputStream istream = ((AMedia)media).getStreamData();
String fileName = ((AMedia)media).getName();
....

link publish delete flag offensive edit

answered 2010-03-22 05:39:31 +0800

techvts gravatar image techvts
120 2 5

hello,
i am also using zk 3.6.3 it doesn't work fine for me.
plz give full source code.

thank,s for help

link publish delete flag offensive edit

answered 2010-03-22 05:55:08 +0800

rotkad gravatar image rotkad
18

techvts, I store .pdf files in database.
so there is not much more code to show.. only the DB part:

Media media = Fileupload.get();
InputStream istream = ((AMedia)media).getStreamData();
String fileName = ((AMedia)media).getName();
courseDAO.insertPdf(istream, fileName);
istream.close();
----------------------------------------
and in the courseDAO.insertPdr(...) method I do like this:

con = ds.getConnection();
stmt = con.prepareStatement("INSERT INTO material(data,file_name) VALUES (?,?)");
int fileLength = istream.available();
stmt.setBinaryStream(1, istream, fileLength);
stmt.setString(2, fileName);
stmt.executeUpdate();
istream.close();
---------------------------------

the only difference I see in your code that I import another Fileupload im my java class:
import org.zkoss.zul.Fileupload;
but I dont know if it matters.

But still, my code works for 3.6.3 and dont for 5.0.1 :(

link publish delete flag offensive edit

answered 2010-03-22 07:38:19 +0800

techvts gravatar image techvts
120 2 5

hi;
Media media = Fileupload.get();
here media returns null value what should i do?

link publish delete flag offensive edit

answered 2010-03-23 00:41:19 +0800

techvts gravatar image techvts
120 2 5

hi ;

i want to store image in particular folder in Server side but i get media =null every time after selected image file so what is my mistake i couldn't understand plz help.

link publish delete flag offensive edit

answered 2010-03-24 23:22:19 +0800

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

You can refer to zkdemo, and check its source code.

link publish delete flag offensive edit

answered 2010-03-26 02:36:26 +0800

techvts gravatar image techvts
120 2 5

hi jumperchen,

the code from zk demo doesn't work for me.
org.zkoss.util.media.Media media = event.getMedia();
image.setContent(media);

above two lines show error
plz help

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

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