开发者

Using Uploadify to restrict number of Uploads

开发者 https://www.devze.com 2023-04-04 07:48 出处:网络
I would like to know how to restrict the user to upload after certain number of uploads.. I want the user not to upload more than 20 files.After that I need the Uploadify to disable.

I would like to know how to restrict the user to upload after certain number of uploads.. I want the user not to upload more than 20 files.After that I need the Uploadify to disable.

And If he clears the uploads again it needs to be re-enabled.

As of now we have "queueSizeLimit" so th开发者_StackOverflowat he can upload files max to the queueSizeLimit.

This is my code:

    <script type="text/javascript">
        $('#file_upload').uploadify({
            'uploader': 'js/uploadify.swf',
            'script': 'UploadVB.ashx',
            'cancelImg': 'images/cancel.png',
            'folder': '/uploads',
            'queueSizeLimit':20,
            'multi': true,
            'auto': true,
            'onQueueFull': function (event, queueSizeLimit) {
                alert("I'm stuffed, please don't put anymore files in me!");
                return false;
            },
             'onComplete': function (event, ID, fileObj, response, data) {
                          $("#thumbnail").append(response + '&nbsp;')

           }
        });
</script>


Since the uploadify queue is empty again after reloading the page, you may want to consider using Cookies to store locally how many files the user has already uploaded. Before initializing uploadify, you check how many uploads the user has already done. If the Cookie is not set or the Number is 0, you can initialize uploadify without problems. If the Number is >0 but <20, you have to adjust the queueSizeLimit of uploadify to make sure the user doesn't upload more than 20 files. If the number is >=20, you don't even initialize uploadify.

The Oncomplete function of uploadify has to add the number of just uploaded files to the cookie number.

It's no foolproof method but it's easy to implement. There's a jQuery cookie plugin You can Use. https://github.com/carhartl/jquery-cookie

If you really want to make sure that every user can upload exactly 20 files max, you have to do it with server-side-scripting.

Edit: In the end, you get something like this. Note that this is pseudo code and not actual javascript.

uploadedFiles = cookie.get(cookieName);
maxFiles = 20 - uploadedFiles;
if (maxFiles <= 0) { doNothing(); } // already 20 uploads or more
else {
    $('#file_upload').uploadify({
        // ...
        'queueSizeLimit': maxFiles, // if uploadedFiles is 5, maxFiles is 15 etc
        // ...
        'onComplete': function (...) {          
            myNumber++; 
            cookie.set(cookieName, uploadedFiles); // Count up number and save it in the cookie
        }
    });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消