0

Customized error message for to big file uploads

asked 2011-11-10 15:01:04 +0800

valmar gravatar image valmar
925 2 13
http://www.timo-ernst.net

I've set a limit for file uploads via zk.xml

When a user uploads a file which is bigger than the configured value, he get this message:

"the request was rejected because its size (666841914) exceeds the configured maximum (52428800)"

Is there a way to replace this message with something more user friendly?

delete flag offensive retag edit

6 Replies

Sort by ยป oldest newest

answered 2011-11-14 12:25:22 +0800

benbai gravatar image benbai
2228 6
http://www.zkoss.org

Hi valmar,

Below is a work-around by override jq.alert:

ZKFiddle-Link

index.zul
<zk>
<script type="text/javascript">
var oldAlert = jq.alert;
jq.alert = function (msg, opts) {
if (!msg.indexOf('the request was rejected because its size'))
oldAlert(' new message ', opts); // use custom message here
else
oldAlert(msg, opts);
}
</script>
<fileupload label="Upload" maxsize="5" />
</zk>

link publish delete flag offensive edit

answered 2011-11-14 14:57:38 +0800

valmar gravatar image valmar
925 2 13
http://www.timo-ernst.net

Hm, how do I get the values for current and maximum file size?
I want to convert em to mb instead of bytes.

link publish delete flag offensive edit

answered 2011-11-14 17:00:32 +0800

benbai gravatar image benbai
2228 6
http://www.zkoss.org

They are in the 'msg' variable,
please refer to the updated sample:

ZKFiddle-Link

index.zul
<zk>
<script type="text/javascript">
var oldAlert = jq.alert;
jq.alert = function (msg, opts) {
if (!msg.indexOf('the request was rejected because its size')) {
var oldMsg = msg;

// get original number
msg = msg.replace('the request was rejected because its size (', '');
msg = msg.replace(') exceeds the configured maximum (', '_');
msg = msg.replace(')', '');
var v1 = msg.substring(0, msg.indexOf('_'));
var v2 = msg.substring(msg.indexOf('_')+1, msg.length);

// calculate MB
var n1 = (parseInt(v1)/1024/ 1024).toFixed(2) + '-MB';
var n2 = (parseInt(v2)/1024/ 1024).toFixed(2) + '-MB';

// replace original number with MB
oldMsg = oldMsg.replace(v1, n1);
oldMsg = oldMsg.replace(v2, n2);

oldAlert(oldMsg, opts); // use custom message here
}
else
oldAlert(msg, opts);
}
</script>
<fileupload label="Upload" maxsize="5" />
</zk>

link publish delete flag offensive edit

answered 2011-11-14 19:09:06 +0800

valmar gravatar image valmar
925 2 13
http://www.timo-ernst.net

Awesome, thanks a lot!

link publish delete flag offensive edit

answered 2013-09-20 14:49:06 +0800

mabio gravatar image mabio
3 1

updated 2013-09-20 14:51:17 +0800

if someone still need ... I made to files in format zip! It's works perfectly!

zul:

<fileupload id="flArquivo" label="Arquivo" width="100px" upload="true,maxsize=-1" />

java:

public void onUpload$flArquivo2(UploadEvent event) throws IOException {
    int limitBytes = 31457280; // 30 Mb
    int fileSizeBytes = event.getMedia().getStreamData().available();

    if (fileSizeBytes <= limitBytes) {
        // OK

    } else {
        // the request was rejected because its size (...)  exceeds the configured maximum (...)
    }
}

I hope it helps someone!

link publish delete flag offensive edit

answered 2016-08-29 16:35:28 +0800

Sidqi gravatar image Sidqi
1

Thanks to Benbai. You can also put your custom message to a label by using widget. Check this out greenfoarfece.wordpress.com/2016/08/27/customized-error-message-when-uploading-big-file

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: 2011-11-10 15:01:04 +0800

Seen: 533 times

Last updated: Aug 29 '16

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