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

Rad Control Upload Problem

1 Answer 67 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Arun
Top achievements
Rank 1
Arun asked on 30 Jul 2013, 01:45 PM
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();
        } 

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 31 Jul 2013, 10:41 AM
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.
Tags
AsyncUpload
Asked by
Arun
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or