This is a migrated thread and some comments may be shown as answers.

Upload file selection limit not working... e.files.length always returns 1

3 Answers 82 Views
Upload
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
M
Top achievements
Rank 1
M asked on 19 Apr 2012, 12:18 PM

Hi,

I am using Telerik upload control in my MVC3 application. And I wanted to limit the upload files selection to 4. So I used below code.

@(Html.Telerik().Upload()
        .Name("files")  
        .Multiple(true)                             
        .Async(
                async => async
                .Save("Save", "MyController")
                .AutoUpload(false)
                .Remove("Remove", "MyController")                            
            )
            .ClientEvents(events => events
            .OnSelect("onSelect")
            )
   )
  
function onSelect(e) {    
        var files = e.files;
        alert(files.length);
        if (files.length > 4) {
            alert("4 files allowed.");
            e.preventDefault();
        }
    }

But in the Javascript function I am getting the files.length always as 1.

Any reasons why?

 

3 Answers, 1 is accepted

Sort by
0
Accepted
T. Tsonev
Telerik team
answered on 23 Apr 2012, 11:36 AM
Hi,

The onSelect event arguments contain the files that the user has just selected. Previously selected files are not included.

In order to limit the total number of files, you need to handle the onRemove event as well and keep count. Something like:
var totalFiles = 0;

function onSelect(e) {    
    var selectedFiles = e.files.length;
    if (totalFiles + selectedFiles > 4) {
        alert("4 files allowed.");
        e.preventDefault();
    }
    else {
        totalFiles += selectedFiles;
    }
}

function onRemove(e) {
    totalFiles--;
}


We recognize that this is a common requirement and we're looking forward to make it easier by adding an option.

All the best,
Tsvetomir Tsonev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
0
M
Top achievements
Rank 1
answered on 24 Apr 2012, 03:25 PM
Thanks Tsonev,
That means the section mentioned/shown here is misleading 
http://www.telerik.com/help/aspnet-mvc/telerik-ui-components-upload-client-api-and-events.html#OnSelect 


Thanks,
M
0
T. Tsonev
Telerik team
answered on 25 Apr 2012, 08:43 AM
Hi,

Yes. Indeed it is and we'll correct it.

As a token of gratitude for your involvement your Telerik points have been updated.

Greetings,
Tsvetomir Tsonev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
Tags
Upload
Asked by
M
Top achievements
Rank 1
Answers by
T. Tsonev
Telerik team
M
Top achievements
Rank 1
Share this question
or