-
FEATURED COMPONENTS
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 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
hai,
pls tell me the way to upload swf file and use it to show from its directory
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.
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
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);
}
}
}
}
Could you please provide us with a runnable example?
And tell us how to reproduce your issue.
Asked: 2010-03-22 04:29:08 +0800
Seen: 2,223 times
Last updated: Aug 13 '12