RadAsyncUpload File size limit

4 posts, 0 answers
  1. Farjana
    Farjana avatar
    26 posts
    Member since:
    Aug 2013

    Posted 20 Nov 2013 Link to this post

    Hi,
    I have a RadAsyncUpload control, i want to upload the file to target folder only on click of upload button. SO i have st the ManualUpload attribute to false. Actually On clicking upload button, i have a function to find the (total size of target folder + uploaded file size). if it exceeds than 50MB, i need to cancel the upload event. I can find the size, but i dont know how to cancel the upload event in c# code.Help me with this



    Regards,
    Farjana
  2. A2H
    A2H avatar
    177 posts
    Member since:
    Mar 2010

    Posted 20 Nov 2013 Link to this post

    Hi,

    You can use the 
    OnFileUploaded event and if the e.File.ContentLength is greater than 50 MB to set the "IsValid" property to false.

    Check the Validation online demo where a similar functionality is implemented.

    Sample Code
    public void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
        {
            const int MaxTotalBytes = 52428800; // 50 MB
            long totalBytes;
     
            var liItem = new HtmlGenericControl("li");
            liItem.InnerText = e.File.FileName;
     
            if (totalBytes < MaxTotalBytes)
            {
                // Total bytes limit has not been reached, accept the file
                e.IsValid = true;
                totalBytes += e.File.ContentLength;
            }
            else
            {
                // Limit reached, discard the file
                e.IsValid = false;
            }
     
            if (e.IsValid)
            {
     
                ValidFiles.Visible = true;
                ValidFilesList.Controls.AddAt(0, liItem);
     
            }
            else
            {
     
                InvalidFiles.Visible = true;
                InValidFilesList.Controls.AddAt(0, liItem);
            }
        }

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Farjana
    Farjana avatar
    26 posts
    Member since:
    Aug 2013

    Posted 25 Nov 2013 Link to this post

    Hello,

    Thanks for suggesting the solution. But the solution u gave validates the entire size in client side while uploading the files. What i actually need is, I have some files to be attached at different times for the same order. The overall size of uploads should not be more than 50MB, so it should first calculate the size of folder in directory in the server and while uploading a new file to the same order, it should check if the sum of files already uploaded exceeds more than 50MB. If so, it should not upload the file. Suggest any event to cancel the upload after knowing that the sum of uploads exceeds 50MB.
    Thanks.
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 26 Nov 2013 Link to this post

    Hi Farjana,

    Please have a look into the following code snippet to cancel the upload when maximum file size reaches.

    ASPX:
    <telerik:RadAsyncUpload ID="RadAsyncUpload1" runat="server" TargetFolder="Uploads"
        OnFileUploaded="RadAsyncUpload1_FileUploaded" OnClientFileUploading="OnClientFileUploading">
    </telerik:RadAsyncUpload>
    <telerik:RadButton ID="RadButton1" runat="server" Text="Save File">
    </telerik:RadButton>
    <asp:HiddenField ID="HiddenField1" runat="server" Value="0" />

    C#:
    protected void RadAsyncUpload1_FileUploaded(object sender, Telerik.Web.UI.FileUploadedEventArgs e)
    {
        string path = Server.MapPath(RadAsyncUpload1.TargetFolder);
        DirectoryInfo dir = new DirectoryInfo(path);
        long size= dir.EnumerateFiles("*", SearchOption.AllDirectories).Sum(fi => fi.Length);
        if (size > 52428800)
        {
            e.IsValid = false;
            HiddenField1.Value = "1";
        }
        else
        {
            e.File.SaveAs(Path.Combine(Server.MapPath(RadAsyncUpload1.TargetFolder), e.File.FileName));
        }
    }

    JavaScript:
    <script type="text/javascript">
        function OnClientFileUploading(sender, args) {
            var hidden = document.getElementById("HiddenField1").value;
            if (hidden == "1") {
                args.set_cancel(true);
                alert("Maximum File Size reaches");
            }
        }
    </script>

    Thanks,
    Shinu.
Back to Top