RadAsyncUpload check file exist on client side

6 posts, 0 answers
  1. Alaaeddin
    Alaaeddin avatar
    4 posts
    Member since:
    Sep 2012

    Posted 04 Jan 2013 Link to this post


    am using RadAsyncUpload to upload flles to the server and want to check if file name exist on client side then javascript popup to confirm overwrite or change file name by adding +1 to the filename.

    I tried to use these script on server side and also didn't goes well for me .


    Protected Sub RadAsyncUpload1_FileUploaded(ByVal sender As Object, ByVal e As FileUploadedEventArgs)
           Dim targetFolder As String = "~/Files"
     
           Dim targetFileName As String = Path.Combine(Server.MapPath(targetFolder), e.File.GetName())
           If Not File.Exists(targetFileName) Then
               e.File.SaveAs(targetFileName)
               txtpath.Text = targetFileName
               ' Implement logic
           Else
     
     
               lbresult.Text = "failed to upload"
     
     
           End If
       End Sub




    and also this code :

     
    Private Sub RadAsyncUpload1_FileUploaded(ByVal sender As Object, ByVal e As FileUploadedEventArgs)
                Dim fullPath As String = Server.MapPath(Path.Combine(RadAsyncUpload1.TargetFolder, e.File.GetName()))
     
                If File.Exists(fullPath) Then
                    e.IsValid = False
                    e.File.SaveAs(Server.MapPath(Path.Combine(RadAsyncUpload1.TargetFolder, e.File.GetNameWithoutExtension() & "1" & e.File.GetExtension())))
     
                txtpath.Text = "http://www.xxxxx.org/Files/" & e.File.FileName
     
            End If
            End Sub

  2. Shoaib
    Shoaib avatar
    9 posts
    Member since:
    Jul 2010

    Posted 09 Jan 2013 Link to this post


    I am unable to check file existing on my current directory , if file exist then add +1 , please tell me any solution .... I found solution with RadUpload , while I am unable to find any solution :/ , my code is mentioned below ..............



    <aspx code>
    <div class="qsf-fb">
                <ul>
                    <li>
                        <telerik:RadAsyncUpload runat="server" ID="RadAsyncUpload1" AllowedFileExtensions="jpg,jpeg,png,gif,doc,docx,xls,xlsx"
                            MaxFileSize="524288"  
    OnFileUploaded="RadAsyncUpload1_FileUploaded"
                            InputSize="30" Width="250" InitialFileInputsCount="1" MaxFileInputsCount="5" Skin="Metro"  
                            OverwriteExistingFiles="false" TargetFolder="D:\\test\\" 
                            TargetPhysicalFolder="D:\\test\\"  >
                        </telerik:RadAsyncUpload>
                    </li>
                </ul>
                <%--<p>
                    <asp:Button runat="server" ID="BtnSubmit" Text="Submit" />
                </p>--%>
                <p class="note">
                    Select images to upload (jpg, jpeg, png, gif,doc,docx,xls,xlsx)<br />
                    File size limit 500 KB, 1 MB total.
                </p>
                <div class="ErrorHolder"></div>
            </div>

            <div class="qsf-results">
                <asp:PlaceHolder ID="ValidFiles" Visible="false" runat="server">
                    <strong>Files upload successfully:</strong>
                </asp:PlaceHolder>
                <asp:PlaceHolder ID="InvalidFiles" Visible="false" runat="server">
                    <strong>The following files were not saved due to exceeding the maximum total size:</strong>
                </asp:PlaceHolder>
            </div>

    ---------------------------------------
    <code behind>

    public void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
            {

                Label fileName = new Label();
                fileName.Text = (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;
                    ValidFiles.Controls.Add(fileName);
                    BindResults();
                }
                else
                {
                    InvalidFiles.Visible = true;
                    InvalidFiles.Controls.Add(fileName);
                }
            }


            private void BindResults()
            {
                if (RadAsyncUpload1.UploadedFiles.Count > 0)
                {
                    labellNoResults.Visible = false;
                    reportResults.Visible = true;
                    reportResults.DataSource = RadAsyncUpload1.UploadedFiles;
                    reportResults.DataBind();
                }
                else
                {
                    labellNoResults.Visible = true;
                    reportResults.Visible = false;
                }
            }

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 09 Jan 2013 Link to this post

    Hi,

    Try the following code to achieve your scenario.
    C#:
    protected void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
    {
      foreach (UploadedFile NewFile in RadAsyncUpload1.UploadedFiles)
        {
            string strPath = Server.MapPath(RadAsyncUpload1.TargetFolder);
     
            FileInfo file = new FileInfo(strPath + "/" + NewFile.FileName);
            if (file != null)
            {
                e.File.SaveAs(strPath + "/" + NewFile.GetNameWithoutExtension() + "1"+ NewFile.GetExtension());
            }
     
        }
    }

    Thanks,
    Shinu.
  5. Shoaib
    Shoaib avatar
    9 posts
    Member since:
    Jul 2010

    Posted 09 Jan 2013 Link to this post

    thankss alot. I resolved .
  6. Alaaeddin
    Alaaeddin avatar
    4 posts
    Member since:
    Sep 2012

    Posted 11 Jan 2013 Link to this post

    The control is invisible now , I can see it in the Design mode but when upload to server the page is blank with no control.
    Also how to read the final filename after the change ? how to trigger upload action ?

    Can you please write the full code ?
  7. Peter Filipov
    Admin
    Peter Filipov avatar
    1028 posts

    Posted 16 Jan 2013 Link to this post

    Hello Alaaeddin,

    My suggestion in to ask the user for that confirmation before the file is being uploaded. E.g. "Do you want to overwrite existing files or save them under new names."  After a postback you will now users' decision and will handle the cases in FileUploaded server side event.

    Regards,
    Peter Filipov
    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017