Stop Upload of Files

Thread is closed for posting
5 posts, 0 answers
  1. SKande
    SKande avatar
    27 posts
    Member since:
    May 2013

    Posted 24 Jul 2013 Link to this post

    Hello,
     I want to validate file extension and ask user in a alert to continue or cancel upload with a invalid file extension if they choose to continue i need to upload files and if they choose cancel, I need to cancel the upload but how to have control  the uplaoding process or do we have any in built method to call stop upload on click of cancel by user.

    Thank you

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 25 Jul 2013 Link to this post

    Hi JJ,

    Please have a look into the sample code I tried which works fine at my end. In the following sample code instead of setting the AllowedFileExtensions in the mark-up, I am explicitly checking the file extensions in the OnClientFileSelected client side event.

    ASPX:
    <telerik:RadUpload ID="RadUpload1" runat="server" OnClientFileSelected="checkExtension">
    </telerik:RadUpload>
    <br />
    <telerik:RadButton ID="RadButton1" runat="server" OnClick="RadButton1_Click" Text="Upload Files">
        <Icon PrimaryIconCssClass="rbUpload" />
    </telerik:RadButton>
    <br />
    <asp:Label ID="Label1" runat="server">
    </asp:Label>

    JavaScript:
    <script type="text/javascript">
        function checkExtension(radUpload, eventArgs) {
            //accessing the current file input field
            var input = eventArgs.get_fileInputField();
            //Extracting the file extension from the path
            var extension = input.value.split('.').pop();
            //Asking the user whether he need to upload the selected file if its not of .jpg extension
            if (extension != "jpg") {
                //Getting the true or false value from the confirm box.
                var result = confirm(input.value.replace(/^.*[\\\/]/, '') + " has an invalid extension. Continue Uploading?");
                if (result == false) {
                    //if the user click Cancel then find the corresponding file input and remove the selected file.
                    var inputs = radUpload.getFileInputs();
                    for (i = 0; inputs.length > i; i++) {
                        if (inputs[i] == input) {
                            radUpload.clearFileInputAt(i); //removing selected file.
                        }
                    }
                }
            }
        }
    </script>

    C#:
    protected void RadButton1_Click(object sender, EventArgs e)
    {
        int count = RadUpload1.UploadedFiles.Count;
        Label1.Text = count + " Files Uploaded<br/><br/>";
        foreach (UploadedFile file in RadUpload1.UploadedFiles)
        {
     
            file.SaveAs(Path.Combine(Server.MapPath("~/Images/Img/"), file.GetName()), true);
        }
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. SKande
    SKande avatar
    27 posts
    Member since:
    May 2013

    Posted 25 Jul 2013 Link to this post

            Hello Shinu,

      Thankyou for the reply but here I want to stop the entire process or cancel the upload itself how can i achieve that.

  5. SKande
    SKande avatar
    27 posts
    Member since:
    May 2013

    Posted 25 Jul 2013 Link to this post

    How can we stop uploading if they cancel that file I want to stop the upload
  6. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 26 Jul 2013 Link to this post

    Hi JJ,

    Please have a look at this thread which deals with a similar scenario. Still you can have a look at the following code I tried with RadUpload in which if the user select an invalid file in between, the entire files and file inputs are deleted hence preventing the further upload process.

    JavaScript:
    <script type="text/javascript">
        function checkExtension(radUpload, eventArgs) {
            //accessing the current file input field
            var input = eventArgs.get_fileInputField();
            //Extracting the file extension from the path
            var extension = input.value.split('.').pop();
            //Asking the user whether he need to upload the selected file if its not of .jpg extension
            if (extension != "jpg") {
                //Getting the true or false value from the confirm box.
                var result = confirm(input.value.replace(/^.*[\\\/]/, '') + " has an invalid extension. Continue Uploading?");
                if (result == false) {
                    //if the user click Cancel then find the corresponding file input and remove the selected file.
                    for (var i = radUpload.getFileInputs().length - 1; i >= 0; i--) {
                        if (i != 0) {
                            radUpload.deleteFileInputAt(i);
                        }
                        else {
                            radUpload.clearFileInputAt(i);
                        }
                    }
                }
            }
        }
    </script>

    Thanks,
    Shinu.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017