This is a migrated thread and some comments may be shown as answers.

Stop Upload of Files

4 Answers 46 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
SKande
Top achievements
Rank 2
SKande asked on 24 Jul 2013, 03:44 PM
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

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 25 Jul 2013, 11:22 AM
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.
0
SKande
Top achievements
Rank 2
answered on 25 Jul 2013, 07:44 PM

        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.

0
SKande
Top achievements
Rank 2
answered on 25 Jul 2013, 07:46 PM
How can we stop uploading if they cancel that file I want to stop the upload
0
Shinu
Top achievements
Rank 1
answered on 26 Jul 2013, 10:42 AM
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.
Tags
Upload (Obsolete)
Asked by
SKande
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 1
SKande
Top achievements
Rank 2
Share this question
or