how to set total Upload file size limit.

Last post 08-25-2011, 1:03 AM by Jayesh Goyani. 2 replies.
Sort Posts: Previous Next
  •  08-24-2011, 12:42 AM 69785

    how to set total Upload file size limit.

    Hello,
     
    I want to upload multiple files but SUM of selected file's size is not greater then 100MB.
     
    I found below Solution but it was not achieve my requirement.
     
     function CuteWebUI_AjaxUploader_OnSelect(files) {
                    var uploader = document.getElementById('<%=Uploader1.ClientID %>');
                    var selectedSize = 0;
                    for (var i = 0; i < files.length; i++) {
                        selectedSize += files[i].FileSize;
                        
                    }
                    if (selectedSize > 104857600) {
                        alert("you only can upload 100MB files.");
                        return false;
                    }
                } 
     
    Issue in Above code :
    First  i select 5 files and each file size is 10MB so totalSize : 50MB
    then again i select 7 files and each file size is  10MB so totalsize of selected files is 70MB and it allowed to me upload.
     
    But Total file size : 50 + 70 = 120MB ....and my TotalSizeLimit is 100MB.....Issue is here.
     
    Problem : it is not checked the file size of previosly selected files.
     
    How can i solve this thing?
     
    Thanks in advance.
  •  08-24-2011, 8:37 AM 69802 in reply to 69785

    Re: how to set total Upload file size limit.

    Hi jgoyani,
     
    Please try the example below
     
    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

        protected void attachment1_AttachmentRemoveClicked(object sender, AttachmentItemEventArgs args)
        {
           
                hfTotalSize.Value=(int.Parse(hfTotalSize.Value)-args.Item.FileSize).ToString();
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <CuteWebUI:UploadAttachments ID="attachment1" runat="server" OnAttachmentRemoveClicked="attachment1_AttachmentRemoveClicked">
            </CuteWebUI:UploadAttachments>
            <asp:HiddenField ID="hfTotalSize" runat="server" Value="0" />
            </form>
    </body>
    </html>

    <script>
    var totalSize = document.getElementById("<%=hfTotalSize.ClientID %>");;
    //max size setting 1024000=1mb
    var MaxSizeKB=1024000;
    function CuteWebUI_AjaxUploader_OnSelect(files) {

        for (var i = 0; i < files.length; i++) {
            totalSize.value = parseInt(totalSize.value) + files[i].FileSize;
        }

        if (parseInt(totalSize.value) > MaxSizeKB) {
            for (var j = 0; j < files.length; j++) {
                totalSize.value = parseInt(totalSize.value) - files[j].FileSize;
            }
            //handle the error message here
            alert("File size limit exceeded");
            return false;
        }

    }
    </script>
     
    Regards,
     
    Ken
     
  •  08-25-2011, 1:03 AM 69809 in reply to 69802

    Re: how to set total Upload file size limit.

    Thanks.
    its work fine.
View as RSS news feed in XML