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

RadAsyncUpload check file exist on client side

5 Answers 877 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Alaaeddin
Top achievements
Rank 1
Alaaeddin asked on 05 Jan 2013, 03:16 AM

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

5 Answers, 1 is accepted

Sort by
0
Shoaib
Top achievements
Rank 1
answered on 09 Jan 2013, 07:58 AM

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;
            }
        }

0
Shinu
Top achievements
Rank 2
answered on 09 Jan 2013, 10:31 AM
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.
0
Shoaib
Top achievements
Rank 1
answered on 10 Jan 2013, 04:51 AM
thankss alot. I resolved .
0
Alaaeddin
Top achievements
Rank 1
answered on 11 Jan 2013, 01:46 PM
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 ?
0
Peter Filipov
Telerik team
answered on 16 Jan 2013, 08:27 AM
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.
Tags
AsyncUpload
Asked by
Alaaeddin
Top achievements
Rank 1
Answers by
Shoaib
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Alaaeddin
Top achievements
Rank 1
Peter Filipov
Telerik team
Share this question
or