Limit upload to a single file

May 26, 2010 at 11:33 AM



I need to be able to limit the user to only uploading a single file for a particular screen in the system.  I was hoping to be able to use the StartUpload method and check the number of files that have been selected there but cannot get the javascript to fire, if I add _slCtl.Content.Files.StartUpload = StartUpload into the plugin loaded method I get an error.  I am using the SL4 version of the control, any ideas how I can implement this?


May 26, 2010 at 12:04 PM

slCtl.Content.Files.StartUpload is not a JavaScript function that's available.
See the JavaScript TestPage included in the project for all the javascript functionality.

If you want to upload only 1 file, choosing the Multi File Uploader is not the best choice :)
There's a little workaround that might be possible (using JavaScript):

Listen to the slCtl.Content.Files.FileAdded (triggers when a new file is added)
If there's more than 1 file in the file list (slCtl.Content.Files.FileList.length)
Show an error (alert('Not allowed to add more files')
Clear the file list (slCtl.Content.Control.ClearList();)

The first file will then also be gone, so the user has to select is again.

Hope this helps!

May 28, 2010 at 10:17 AM
Edited May 28, 2010 at 1:37 PM

Thanks that works, but slightly annoying for the users!

Is there anyway to manipulate the file list from JavaScript, other than just clearing it?  Wondering if I cleared the list that I could re-add the first file in the list via JavaScript, or to remove all but the first file?

There is another section of the website that does allow for multiple uploads, which is why this control is being used, so it seemed to make sense to use the same control in both places...


Just extended the control to add a MaxFiles property that can be set, and checked when the user clicks the upload button, also added a RemoveLastItem method to allow the last file to be removed from the list.

May 28, 2010 at 11:22 AM

Hi Rupert,

I just checked in a change I made for you. You can now call RemoveAt(int index) on the file collection on JavaScript.
A sample implementation is included in the JavaScript example page when you download the last checkin (not the last release):

So now you can make a javascript function like this

Listen to the slCtl.Content.Files.FileAdded (triggers when a new file is added)
If there's more than 1 file in the file list (slCtl.Content.Files.FileList.length)
Remove everything except the first one (while slCtl.Content.Files.FileList.length > 1
 slCtl.Content.Control.RemoveAt(1); (removed the second file)

That should work for you I think. It has a better user experience, because you can keep the first file.

May 28, 2010 at 2:00 PM

Thanks, as I mentioned above I have added some code into the SilverLight control to allow a MaxFiles property to be set, and then this is checked whenever files or added, or the user attempts to upload the files.

Happy to send in the code if you want.

Cheers, Rupert