0

How to save Fileupload media to disk

asked 2008-12-14 14:20:06 +0800

electron34 gravatar image electron34
30 1

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.

delete flag offensive retag edit

8 Replies

Sort by ยป oldest newest

answered 2008-12-14 16:28:49 +0800

mjablonski gravatar image mjablonski
1284 3 5
http://www.jease.org/

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

link publish delete flag offensive edit

answered 2008-12-14 17:07:40 +0800

electron34 gravatar image electron34
30 1

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.

link publish delete flag offensive edit

answered 2008-12-14 17:33:05 +0800

mjablonski gravatar image mjablonski
1284 3 5
http://www.jease.org/

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

link publish delete flag offensive edit

answered 2008-12-14 17:38:50 +0800

electron34 gravatar image electron34
30 1

Brilliant. Works perfect. Thank you mjablonski.

link publish delete flag offensive edit

answered 2010-02-07 19:27:40 +0800

xiaopingbeyond gravatar image xiaopingbeyond
24 1

perfect!! Thank you mjablonski.

link publish delete flag offensive edit

answered 2010-03-23 00:57:01 +0800

techvts gravatar image techvts
120 2 5

i want to store image file to folder on server what should i do in this code to change i tried this code but it gives media =null.
plz help

link publish delete flag offensive edit

answered 2013-11-12 14:22:18 +0800

majose1006 gravatar image majose1006
0

mjablonski ..

Thank You!!!!!!

link publish delete flag offensive edit

answered 2018-11-13 17:01:47 +0800

rahul123 gravatar image rahul123
1 1

updated 2018-11-15 09:25:18 +0800

cor3000 gravatar image cor3000
6280 2 7

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

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: 2008-12-14 14:20:06 +0800

Seen: 1,212 times

Last updated: Nov 15 '18

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