First time here? Check out the FAQ!
<?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
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();
....
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 :(
You can refer to zkdemo, and check its source code.
Could you please provide us with a runnable example?
And tell us how to reproduce your issue.
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);
}
}
}
}
Asked: 2010-03-22 04:29:08 +0800
Seen: 2,223 times
Last updated: Aug 13 '12