0

onUpload Event

asked 2009-06-22 21:05:08 +0800

james gravatar image james
255 2

In a grid, I am displaying the file contents of a folder.
I want to refresh the display when I upload a file.
I was trying to use the onUpload event, but it doesn't do anything.
sample code from file.zul

<window id="uploadWin" width="200px" border="normal" use="com.UploadForm">
	<caption label="Upload Files" />
	<hbox>
		<button label="Upload" onClick="uploadWin.upload(uploadPath)">
			<attribute name="onUpload">
				alert("file has been uploaded");
			</attribute>
		</button>
	</hbox>
</window>

code from UploadForm.java
public void upload(String path) {
	try {
		Media[] media = Fileupload.get(-1);
		if (media != null) {
			for (int i = 0; i < media.length; i++) {
				String uploadedfile = media<i >.getStringData();	
				Sql sqlForSlash = new Sql();
				OutputStream out = new FileOutputStream(this.getCurrentTreePath() + media<i >.getName());
				out.write(uploadedfile.getBytes());
				out.close();
			}
		}
	} catch (Exception e) {
		showError("ERROR " + e.getMessage());
	}
}

When I use this, I get no alert/message box. The file uploads just fine, no problems. But the onUpload Event is obviously not called.
Does anyone know of something I can do or use to execute code when I upload a file?

Thanks to everyone who takes the time to look at this, and I greatly appreciate any help I can get.
-James

delete flag offensive retag edit

4 Replies

Sort by ยป oldest newest

answered 2009-06-22 22:37:26 +0800

deved gravatar image deved
114 1

Hi James,
The "onUpload" event is available if you use the fileupload component:

<fileupload onUpload="alert(..)" />

But in your case, you use the Fileupload.get method, so if you need to display a message after the upload process has been completed you can do this:
Media[] media = Fileupload.get(-1);
		if (media != null) {
			for (int i = 0; i < media.length; i++) {
				String uploadedfile = media<i >.getStringData();	
				Sql sqlForSlash = new Sql();
				OutputStream out = new FileOutputStream(this.getCurrentTreePath() + media<i >.getName());
				out.write(uploadedfile.getBytes());
				out.close();
			}
		}
    Messagebox.show("file has been uploaded");

Also note that you are registering the "onUpload" event for a button, the button doesn't fire this kind of event.

Regards,
edgar

link publish delete flag offensive edit

answered 2009-06-22 23:03:38 +0800

james gravatar image james
255 2

Thanks Edgar.
(The message to be displayed was only for testing, to see if the event even occured.)
In that case I have another question, how would I be able to interact with tree and grid components?
file.zul

<hbox>
<vbox>
<tree width="250px" id="dirTree" Zclass="z-filetree">
   	<attribute name="onSelect">
   	    if (!dirTree.getSelectedItem().getValue().equals("")){
   	        main.setCurrentTreePath(dirTree.getSelectedItem().getValue().toString());
			
		ArrayList fileDataList = main.fileDataList(dirTree.getSelectedItem().getValue().toString());
		ListModel fileDataListModel = new SimpleListModel(fileDataList.toArray());
		viewGrid.setModel(fileDataListModel); 
		viewGrid.setRowRenderer(new dir_cont.FilelistRowRenderer());
	    }
	</attribute>
</tree>
<window id="uploadWin" width="200px" border="normal" use="com.UploadForm">
	<caption label="Upload Files" />
	<hbox>
		<button label="Upload" onClick="uploadWin.upload(uploadPath)">
			<attribute name="onUpload">
				alert("file has been uploaded");
			</attribute>
		</button>
	</hbox>
</window>
</vbox><vbox>
<grid id="viewGrid" mold="paging" pageSize="30" pagingPosition="both">
	<columns sizable="true">
		<column label="File Name" width="260px" />
		<column label="Download" width="70px" align="center" />
		<column label="Delete" width="60px" align="center" />
		<column label="File Size" width="70px" align="right"/>
	</columns>
</grid>
<zscript>
	dirTree.setModel(main.createDirectoryTreeModel());
	dirTree.setTreeitemRenderer(new dir_tree.DirTreeitemRenderer());
</zscript>
</vbox>
</hbox>

I have tried the following in my UploadForm.java...
ArrayList<Filelist> fileDataList = sqlClass.fileDataList(((Tree) this.getFellow("dirTree")).getSelectedItem().getValue().toString());
ListModel fileDataListModel = new SimpleListModel(fileDataList.toArray());
((Grid) this.getFellow("viewGrid")).setModel(fileDataListModel); 
((Grid) this.getFellow("viewGrid")).setRowRenderer(new FilelistRowRenderer());

and
ArrayList<dir_cont.Filelist> fileDataList = sqlClass.fileDataList(((Tree) this.getSpaceOwner().getFellow("dirTree")).getSelectedItem().getValue().toString());
ListModel fileDataListModel = new SimpleListModel(fileDataList.toArray());
((Grid) this.getSpaceOwner().getFellow("viewGrid")).setModel(fileDataListModel); 
((Grid) this.getSpaceOwner().getFellow("viewGrid")).setRowRenderer(new dir_cont.FilelistRowRenderer());

This code is placed after the file is uploaded. But I get the following Exception error message...
Fellow component not found: dirTree

Then, if I manually refresh the view, the file is there. But I want the view to be refreshed automatically.

Anyone have any ideas or suggestions about this?
Thank you
-James

link publish delete flag offensive edit

answered 2009-06-22 23:16:26 +0800

deved gravatar image deved
114 1

Hi James,
The "dirTree" component is out of the scope of the "uploadWin" component.
You have this:

   <tree id="dirTree"/>
   <window id="uploadWin">
 

Then you are trying to access with this:
this.getFellow("dirTree")/this.getSpaceOwner().getFellow("dirTree")

I think that the "getSpaceOwner" method on a Window component return the window component itself and because the "dirTree" is not a child of "uploadWin" you get the exception.
Try this: "this.getParent().getFellow("dirTree")" or "this.getParent().getSpaceOwner().getFellow("dirTree")".

Regards,
edgar

link publish delete flag offensive edit

answered 2009-06-22 23:36:07 +0800

james gravatar image james
255 2

Edgar,
Thank you very much. That code worked wonderfully.
this.getParent().getSpaceOwner().getFellow("dirTree") gets the correct component and executes the code correctly.
Thank you again.
-James

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: 2009-06-22 21:05:08 +0800

Seen: 534 times

Last updated: Jun 22 '09

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