0

Upload csv files with Zk framework

asked 2013-09-25 21:23:45 +0800

daovallec gravatar image daovallec
11 2

I need upload csv files in ZK this is my zul page:

<zk>
<window
    apply="org.zkoss.bind.BindComposer"
    viewModel="@id('vm') @init('UploadVM')"
    title="win"
    position="center"
    mode="overlapped"
    border="normal"
    width="300px"
    height="200px">
    <button
        label="upload"
        upload="true,maxsize=801192"
        onUpload="@command('uploadFile',upload=event)"
        autodisable="self" />

</window>
</zk>

And my java page:

public class UploadVM {
    private Media media;
    public Media getMedia(){
        return media;
    }
    @NotifyChange("media")
    @Command
    public void uploadFile(@ContextParam(ContextType.TRIGGER_EVENT) UploadEvent event) {
        media = event.getMedia();
        media.getStreamData();
    }

But with this simple example i have the following error:

 Use getStringData() instead

And i don not know what happen.

Who can help me???

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2013-09-26 15:38:46 +0800

sraul gravatar image sraul flag of Paraguay
32 3

updated 2013-09-30 06:14:25 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

//THE UI COMPONENT.. SHOULD HAVE AN ID

fileupload id="csvUpload" width="110px"
label="Upload csv file" style="font-size:11px"
image="/core/images/csv.png"

//THIS SHOULD BE IN THE ZUL VM TO LISTEN THE EVENT'S..

@AfterCompose
public void afterCompose(@ContextParam(ContextType.VIEW) Component view){
Selectors.wireEventListeners(view, this);
}

//THE METHOD THAT UPLOAD THE FILE SELECTED..    
@Listen("onUpload=#csvUpload")
public void upload(UploadEvent event){
    String name = 'THE FILE NAME';
    String path = 'THE PAT OF THE FILE'; //like this: Sessions.getCurrent().getWebApp()                                        //.getRealPath("/resources/csvFiles/") + "/";                
   this.uploadFile(path, name, ".csv", event.getMedia().getStreamData());       
        BindUtils.postNotifyChange(null, null, this, "*");
}


//THE METHOD THAT READ THE FILE

public void uploadFile(String path, String name, String ext,
            InputStream file) {
    try {
        OutputStream out = new java.io.FileOutputStream(path + name + ext);
        InputStream in = file;
        int read = 0;
        byte[] bytes = new byte[1024];
        while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            in.close();
            out.flush();
            out.close();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

//THIS WORKS FOR ME

link publish delete flag offensive edit
0

answered 2013-09-30 06:16:30 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

See if this zk-upload-image-or-image-binding-in you have to change the Image to CSV in code little bit change.

link publish delete flag offensive edit
Your answer
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
1 follower

RSS

Stats

Asked: 2013-09-25 21:23:45 +0800

Seen: 55 times

Last updated: Sep 30 '13

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