Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » upload file size warning?

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2
Go to End
Author Topic: 2900 Views
  • janulka
    Avatar
    Community Member
    80 Posts

    upload file size warning? Link to this post

    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.

  • UncleCheese
    Avatar
    4085 Posts

    Re: upload file size warning? Link to this post

    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'

  • janulka
    Avatar
    Community Member
    80 Posts

    Re: upload file size warning? Link to this post

    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?

  • UncleCheese
    Avatar
    4085 Posts

    Re: upload file size warning? Link to this post

    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.

  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: upload file size warning? Link to this post

    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 this answer 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.

  • UncleCheese
    Avatar
    4085 Posts

    Re: upload file size warning? Link to this post

    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.

  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: upload file size warning? Link to this post

    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?

  • UncleCheese
    Avatar
    4085 Posts

    Re: upload file size warning? Link to this post

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

    2900 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.