-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I have tried to Save the Content of the media to disk, but failed. I have tried to cast it to Object and save it but failed. Does anyone know how to save the content.
Just create a Java-Utiltity which handles the task for you:
import java.io.File; import java.io.FileOutputStream; import org.zkoss.zul.Fileupload; public class Upload { public static void saveToDisk() throws Exception { org.zkoss.util.media.Media media = Fileupload.get(); FileOutputStream fos = new FileOutputStream(new File(media.getName())); fos.write(media.getByteData()); fos.close(); } }
So you can trigger a upload from ZUL via:
<button label="Upload" onClick="Upload.saveToDisk()" />
HTH, Maik
I have tried that, i get a message telling me that i must use getStreamData() instead.
How do i get the content from this method. I am using ZK 3.0.5.
Something like this should work directly with InputStreams:
public static void saveToDisk() throws Exception { org.zkoss.util.media.Media media = Fileupload.get(); OutputStream outputStream = new FileOutputStream(new File(media.getName())); InputStream inputStream = media.getStreamData(); byte[] buffer = new byte[1024]; for (int count; (count = inputStream.read(buffer)) != -1;) { outputStream.write(buffer, 0, count); } outputStream.flush(); outputStream.close(); inputStream.close(); }
HTH, Maik
Brilliant. Works perfect. Thank you mjablonski.
perfect!! Thank you mjablonski.
try this ,i was also getting 'null' earlier.
<!-- zk -->
<button id="uploadBtn" label="Upload file" upload="true" />
........................
//java
@Listen("onUpload=#uploadBtn")
public void uploadImage(UploadEvent event) {
msgLb.setValue("");
try {
Media media = event.getMedia();
if(media == null){
msgLb.setValue("please select a file");
return;
}
rest code same
Asked: 2008-12-14 14:20:06 +0800
Seen: 1,212 times
Last updated: Nov 15 '18