Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

upload file size warning?


Go to End
Reply


9 Posts   3032 Views

Avatar
janulka

Community Member, 80 Posts

19 January 2010 at 11:08am

Edited: 19/01/2010 11:08am

Hello,

I made website on 2.3.3, DataObjectManager and ImageGallery. Clients got instruction about recommended file size of images they upload. Not always "regular" people are so concerned about file size, and it happened that they uploaded several MB image, and they got memory limit fatal error..

Well, would it somehow be possible to display some kind of warning when clients are trying to upload file size larger then recommended size?

I do not want to limit upload_max_filesize..

Thanks in advance.

Avatar
UncleCheese

Forum Moderator, 4094 Posts

19 January 2010 at 11:30am

In the SWFUpload module, the file size is set to your upload_max_filesize setting in PHP. To change it, just add a line to your _config.php:

SWFUploadConfig::set_var('file_size_limit','2MB');

That may not work, because I think the _config.php files are executed in alphabetical order, so swfupload comes toward the end, which will overwrite the change if your code is in the mysite folder. In that case, just go to the swfupload/_config.php and change the setting on line 10.

'file_size_limit' => '2MB'

Avatar
janulka

Community Member, 80 Posts

20 January 2010 at 10:32am

thanks a lot for reply!

that's the problem - I do not want to limit their upload file size, I just want to give them warning, some kind of: "This file is larger than 2MB, are you sure you want to upload?" -> "Yes I am sure, upload the file" or "Cancel" button.

It is GD and image resize functions which are eating up a lot of memory, but I still want them to be able to upload let's say pdf file which is larger than 2MB for direct download..

Is this kind of warning possible?

Avatar
UncleCheese

Forum Moderator, 4094 Posts

20 January 2010 at 10:55am

Edited: 20/01/2010 10:58am

Sure... it's pretty straightforward.

- First, create a new javascript file that will contain custom handlers for SWFUpload. Let's call it /mysite/javascript/swfupload_handlers.js

- Copy the fileQueued(file) function from /swfupload/javascript/handlers.js into your swfupload_handlers.js file, and rename it myFileQueued(file)

- Modify the function so that it throws a window confirm:

function myFileQueued(file) {
max_size = 2*1024*1024 // 2mb
if(file.size > max_size) {
if(!window.confirm('This is a big file. Are you sure you want to upload it?')) {
removeFileFromQueue(file.id);
return;
}
}
// everything below is the same...    
try {
      li = document.createElement("li");
      li.setAttribute('id', 'file-' + file.id);
      filename = new String(file.name);
      if(filename.length > 30)
         filename = filename.substr(0,29) + '...';
         
      li.innerHTML = "<div class='queue-file-name'>" + filename + "</div><div class='queue-remove-btn'><a href='javascript:void(0);' onclick='return removeFileFromQueue(\""+file.id+"\");'>remove</a></div>";
      txtFileNames.appendChild(li);
      btnSubmit.style.display = 'block';
      var progress = new FileProgress(file, 'file-' + file.id);
      progress.setProgress(0);
      meg = file.size > 1024*1024;
      size = meg ? file.size/1024/1024 : file.size/1024;
      rounded = Math.round(size*10)/10;
      formatted = meg ? rounded : addCommas(Math.ceil(rounded));
      suffix = meg ? 'M' : 'k';
      progress.setStatus('Queued ('+formatted+suffix+')');
      progress.fileProgressElement.childNodes[2].className = 'progressBarStatus queued';
            
   } catch (e) {
   }

}

- Lastly, we need to assign the new handler to SWFUpload. In your getCMSFields() function, include the script, and update the handler.

function getCMSFields() {
LeftAndMain::require_javascript('/path/to/swfupload_handlers.js');
SWFUploadConfig::set_var('file_queued_handler' => 'myFileQueued');
// etc..

}

Note: If you are using the ImagGalleryModule as it comes, you don't have your own getCMSFields() function to modify. In that case, just put it in your Page.php getCMSFields() function. The only downside is that it will apply the change on all your pages, but it sounds like that's okay with you.

Can't promise this is free of syntax errors or even if it will work, but hopefully it pushes you in the right direction.

Avatar
theAlien

Community Member, 131 Posts

29 January 2010 at 12:34pm

Edited: 29/01/2010 1:07pm

Hi,
I've been debugging this one, but still I can't get it working.
That is: everytime I try to implement it, the upload-button disappears, so I can't upload any new images.

At the moment, I narrowed it down to the part that is inserted after the function getCMSFields().

Maybe someone can help me out?

BTW: the bug I found in that part: '=>' should be ',' :

function getCMSFields() {
LeftAndMain::require_javascript('/mysite/javascript/swfupload_handlers.js');
SWFUploadConfig::set_var('file_queued_handler', 'myFileQueued');
// etc..

}

EDIT: I found [url=http://www.silverstripe.org/dataobjectmanager-module-forum/show/275126?start=0#post275236]this answer[/url] to XPD, reporting a similar error. Opening in a new tab doesn't solve anything. Also firebug doesn't report any errors.
EDIT2: Tried it also in IE8, same results, but now there is an error-report:

...
Message: 'myFileQueued' is not defined
Line: 73
Token: 4
Code: 0
URI: http://localhost/ss234/admin/EditForm/field/HeaderObject/item/1/DetailForm/field/HeadImagesObject/upload


Oh, and I triplechecked swfupload_handlers.js: both filename and functionname are right.

Avatar
UncleCheese

Forum Moderator, 4094 Posts

29 January 2010 at 1:46pm

Well it's pretty simple.. your custom handlers aren't being included in the source code. Start by changing the path.. maybe remove the leading slash. That always works for me.

If that doesn't work, just drop the LeftAndMain::require_javascript() to your _config.php.

I would just view the source code, keep making changes and refresh the code until you see that the js has been included. Then go back to the popup window and see if you get some functionality.

Avatar
theAlien

Community Member, 131 Posts

30 January 2010 at 1:55am

Edited: 30/01/2010 2:00am

Thanks, an update:
If I'm adding LeftAndMain::require_javascript() to _config.php the javascript is added to the page.

But... I'm using FDOM as a nestedDOM. And in the nestedDOM-page the javascript is still missing.
(I should have said that before, but I figured just now the nestedDOM-part could be relevant)

Is this a bug or is there a workaround?

Avatar
UncleCheese

Forum Moderator, 4094 Posts

30 January 2010 at 8:15am

I don't see how a nested DOM would make any difference. I'm pretty confused by that one.

Go to Top