Rad Control Upload Problem

2 posts, 0 answers
  1. Arun
    Arun avatar
    13 posts
    Member since:
    May 2013

    Posted 30 Jul 2013 Link to this post

    Hi ,
    I am using Radupload control to upload multiple files in my project.It Working Correctly on uploading single file .But It Is not working Multiple Files.It Upload only one file in multiple times with same name.Please give me the Solution.

    In My aspx page:
     <telerik:RadAsyncUpload runat="server" ID="RadAsyncUpload1" 
                            TargetFolder="~/uploadattachment/" AllowedFileExtensions=".pdf,.doc,.docx,.txt,.gif,.jpg,jpeg,.png"
                            MaxFileSize="524288" Skin="Simple" MultipleFileSelection="Automatic" UploadedFilesRendering="BelowFileInput"
                            ChunkSize="0" Style="margin-right: 110px; margin-top: 10px" OnFileUploaded="RadAsyncUpload1_FileUploaded">
                        </telerik:RadAsyncUpload>

    In My .cs page:

    protected void RadAsyncUpload1_FileUploaded(object sender, Telerik.Web.UI.FileUploadedEventArgs e)
            {       


                string fileid;
                UploadedFile file = RadAsyncUpload1.UploadedFiles[0];
                string filename = file.FileName;
                string pathname = Path.GetFileName(filename);
                string targetfolder = Server.MapPath(RadAsyncUpload1.TargetFolder);
                string folderpath = targetfolder + filename;           
                string temp = "";
                string fname = "";
                int cnt = 1;
                if (File.Exists(folderpath))
                {
                   for (int i = 1; i <= cnt; i++)
                   {
                        string woext = Path.GetFileNameWithoutExtension(filename);
                        string ext = Path.GetExtension(filename);
                        string addnum = woext + "(" + cnt + ")";
                        temp = addnum + ext;
                        if (File.Exists(targetfolder + temp))
                            cnt++;
                   }

                    fname = temp;
                    e.File.SaveAs(Server.MapPath("~/uploadattachment/" + fname));
                    fileid = fname;
                    builder.Append(fileid).Append(",");
                   
                }
                else
                {

                    e.File.SaveAs(Server.MapPath("~/uploadattachment/" + filename));
                    fileid = filename;
                    builder.Append(fileid).Append(",");
                  //  Id = builder.ToString();
                }
                Id = builder.ToString();
            } 
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 31 Jul 2013 Link to this post

    Hi Arun,

    The server-side OnFileUploaded event occurs after a file is uploaded and a postback is triggered. This event continues to fire until all the selected files are uploaded and hence you don't need to loop through the selected files to upload them to server. Also the RadAsyncUpload overwrites the file by default, if the file already exists in the target upload path. Please have a look into the following code I tried to upload multiple files using RadAsyncUpload.

    ASPX:
    <telerik:RadAsyncUpload runat="server" ID="RadAsyncUpload1" PostbackTriggers="RadButton2"
        OnClientFileUploaded="OnClientFileUploaded" TargetFolder="~/Images/Img/" AllowedFileExtensions=".jpg"
        MaxFileSize="57971152" MultipleFileSelection="Automatic" UploadedFilesRendering="BelowFileInput"
        OnFileUploaded="RadAsyncUpload1_FileUploaded">
    </telerik:RadAsyncUpload>
    <br />
    Uploaded Files<br />
    <telerik:RadListBox ID="RadListBox1" runat="server" Visible="true">
    </telerik:RadListBox>
    <br />
    <telerik:RadButton ID="RadButton2" runat="server" Text="Upload" OnClick="RadButton2_Click">
    </telerik:RadButton>

    C#:
    protected void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
    {
        string targetfolder = RadAsyncUpload1.TargetFolder;
        e.File.SaveAs(Path.Combine(Server.MapPath(targetfolder), e.File.FileName));
        RadListBoxItem item = new RadListBoxItem(e.File.FileName);
        RadListBox1.Items.Add(item);
    }

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