0

How to open an upload window without clicking on a component ?

asked 2016-03-18 13:28:02 +0800

caiofilipemr gravatar image caiofilipemr
5 2

Hello everyone, this is my first question. Is it possible open the upload dialog/window without an event ? The user will input some info and after a few validations I open or not a upload dialog, like the one on onUpload event. I am using ZK 6.0.0.

Thanks in advance and sorry for the bad english.

delete flag offensive retag edit

5 Answers

Sort by ยป oldest newest most voted
0

answered 2016-03-21 01:58:44 +0800

cor3000 gravatar image cor3000
4351 2 7
ZK Team

Opening a file upload dialog is a very security sensitive topic. It usually must be triggered by a direct user action e.g. clicking an upload button or dragging a file.

Trying to open the file upload from an AJAX response will usually fail, unless you are using an older browser, or one that's missing the relevant security updates.

More details can be found here: http://stackoverflow.com/a/29873845/4740707

So don't even try, there is a good reason why this does not work.

link publish delete flag offensive edit
0

answered 2016-03-18 13:50:02 +0800

Darksu gravatar image Darksu
1991 1 4

Hello caiofilipemr,

First of all you should listen to the onChange event of the component (textbox) as shown below:

http://books.zkoss.org/wiki/ZKDeveloper'sReference/MVC/Controller/WireEventListeners

Then you could do the following:

Clients.evalJavaScript("jq('$btn').next().find('input').click();");

btn is the actual id of the upload button.

A similar fiddle can be found here:

http://zkfiddle.org/sample/9di1nb/1-Trigger-Upload#source-1

Best Regards,

Darksu

link publish delete flag offensive edit
0

answered 2016-03-21 13:48:07 +0800

caiofilipemr gravatar image caiofilipemr
5 2

Thank you Darksu,

I tried to do what you said, but doesn't work, probably because what cor3000 said (thank you cor), I read the post on StackOverflow and understood why this not work, I will find a new alternative to solve my problem. Thank you both again.

link publish delete flag offensive edit
0

answered 2016-03-21 14:31:03 +0800

Darksu gravatar image Darksu
1991 1 4

Hello caiofilipemr,

Did you try out the fiddle that i was referring to?

Best Regards,

Darksu

link publish delete flag offensive edit
0

answered 2016-03-21 15:25:35 +0800

caiofilipemr gravatar image caiofilipemr
5 2

updated 2016-03-21 15:27:02 +0800

I tried to do like was in the fiddle, if I understood correctly the Clients.evalJavaScript("jq('$btn').next().find('input').click();"); will click in my btn and trigger the onUpload, so I put this code in my .zul: <button id="btn" upload="true" visible="false"/> and this in my .java:

public void myValidationFunction() { //Which is triggered after a button clicked, but I have
if (valid())                         //to make some validations before upload the file
    Clients.evalJavaScript("jq('$btn').next().find('input').click();");
}

public void onUpload$btn(UploadEvent event) {
     System.out.println("test");
}

This doesn't work, maybe I did it wrong, but I think what cor3000 said makes sense. What do you think ?

link publish delete flag offensive edit

Comments

to me it looks good, and I think I have tried essentially the same in the past. Then I found out about the browser security limitations. Executing the same js in a client side event listener will work, while it fails when returned from the server side. I don't know of a way to make it "trusted".

cor3000 ( 2016-03-22 02:07:02 +0800 )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: 2016-03-18 13:28:02 +0800

Seen: 46 times

Last updated: Mar 21 '16

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