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

RadUpload and javascript confirm() for overwriting files

1 Answer 100 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 31 Jan 2012, 06:00 PM

I have a RadWindowManager with RadWindow that opens a new window when a link within a RadGrid cell is clicked.  It opens the new window as a Modal="true".  In the new window's aspx page, it has a RadUpload, RadProgressArea and RadProgressManager, that uses TargetFolder="~/UploadFiles" and OverwriteExistingFiles="false".  What I would like is for it to check to see if the file exists already when the user submits the file.  If so, I want to present a javascript confirm() box to the user to ask if they want to overwrite the file.  If yes, then let it overwrite (and store the extra data in the database), if no, return back to the RadUpload popup window without saving the file (thus allowing them to cancel the process, rename the file on their hard drive and then re-upload).  I have tried using the submit button's click event handler to check for the file's existance, and if so, it calls a ClientScript.RegisterClientScriptBlock to register a confirm() box.  However, it doesn't seem to either fire or attach properly (or some other reason I can't figure out).  I've tried a number of different ways, but none are working.  The current version uses a hidden field to track if the file exists or not, but it's not working.  Any thoughts?
ASPX:

<script type="text/javascript">
        function CloseAndRebind() {
            GetRadWindow().BrowserWindow.refreshGrid();
            GetRadWindow().close();
        }
  
        function GetRadWindow() {
            var oWindow = null;
            if (window.radWindow) oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog
            else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow; //IE (and Moz as well)
  
            return oWindow;
        }
  
        function CancelEdit() {
            GetRadWindow().close();
        }
  
        function ExecuteConfirm() {
            var returnValue = confirm('Warning: File exists.  Wanna overwrite?');
            if (returnValue) {
                document.getElementById('FileExistsHidden').value = "1";
                document.getElementById('SubmitButton').click();
            }
        }
  
        </script>
  
<telerik:RadUpload ID="FileRadUpload" runat="server"
        ControlObjectsVisibility="ClearButtons" InitialFileInputsCount="1" 
        MaxFileInputsCount="1" TargetFolder="~/UploadFiles"
        OverwriteExistingFiles="false" InputSize="45" Width="500px">
    </telerik:RadUpload>
    <telerik:RadProgressArea ID="RadProgressArea1" runat="server"
        DisplayCancelButton="true"
        ProgressIndicators="TotalProgressBar, TotalProgress, RequestSize, FilesCountBar, FilesCount, SelectedFilesCount, CurrentFileName, TimeElapsed, TimeEstimated, TransferSpeed">
    </telerik:RadProgressArea>
    <telerik:RadProgressManager ID="RadProgressManager1" runat="server" />
        <hr />
        <asp:Button ID="SubmitButton" runat="server" Text="Submit" onclick="SubmitButton_Click" />
        <asp:HiddenField ID="FileExistsHidden" runat="server" />
aspx.cs:
protected void SubmitButton_Click(object sender, EventArgs e)
        {
            int customerDataId = MathHelper.ParseIntZeroIfNull(Request.QueryString.Get("cdid"));
            Type cstype = GetType();
            string warningScriptName = "PopupScript";
            string rebindScriptName = "RebindScript";
            ClientScriptManager cs = Page.ClientScript;
              
            if (FileRadUpload.UploadedFiles.Count > 0)
            {
                if (customerDataId > 0)
                {
                    Customers customers = new Customers();
                    foreach (UploadedFile file in FileRadUpload.UploadedFiles)
                    {
                        if (customers.CustomerFileExists(file.GetName()))
                        {
                            if (FileExistsHidden.Value != "1")
                            {
                                if (!cs.IsClientScriptBlockRegistered(SubmitButton.GetType(), warningScriptName))
                                {
                                    cs.RegisterClientScriptBlock(GetType(), SubmitButton.ID, "ExecuteConfirm();", true);
                                }
                            }
                            else
                            {
                                FileExistsHidden.Value = "";
                            }
  
                        }
                        else
                        {
                            customers.InsertCustomerFile(customerDataId, file.GetName(), Portal.GetIdsid);
                        }
                    }
                }
            }
  
            if (!cs.IsClientScriptBlockRegistered(GetType(), rebindScriptName))
            {
                cs.RegisterClientScriptBlock(cstype, rebindScriptName, "CloseAndRebind();", true);
            }
        }

1 Answer, 1 is accepted

Sort by
0
Bozhidar
Telerik team
answered on 01 Feb 2012, 01:19 PM
Hi John,

I've prepared a sample page showing one way you can implement the desired functionality. In the page I use RadAsyncUpload, so that I can take advantage of it's PostBackTriggers property, which gives me the ability to postback the page without losing the state of the upload (required to check if the file exists on the server).

You can use the approach shown in the example and apply it to your scenario.
 
Greetings,
Bozhidar
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Upload (Obsolete)
Asked by
John
Top achievements
Rank 1
Answers by
Bozhidar
Telerik team
Share this question
or