AsyncUpload files format being changed

2 posts, 0 answers
  1. Joshua
    Joshua avatar
    6 posts
    Member since:
    Oct 2014

    Posted 24 Feb Link to this post

    Hello

    I am using AsyncUpload to upload photos and store them in a folder that exists inside my project

            <asp:Label ID="PhotoLabel" runat="server" Text="Upload a photo"></asp:Label>
            <telerik:RadAsyncUpload RenderMode="Lightweight" runat="server" ManualUpload="false"  ID="AsyncUpload1" MultipleFileSelection="Disabled" OnFileUploaded="AsyncUpload1_FileUploaded"></telerik:RadAsyncUpload>
            <telerik:RadButton runat="server" Text="Sibmit new photos" OnClick="UploadFiles_Click"></telerik:RadButton>
            <asp:Label ID="error" runat="server" Text="" Visible="false"></asp:Label>

     

        protected void UploadFiles_Click(object sender, EventArgs e)
        {
            try
            {
                if(AsyncUpload1.UploadedFiles.Count > 0)
                {
                    foreach (UploadedFile file in AsyncUpload1.UploadedFiles)
                    {
                        string targetFolder = HttpContext.Current.Server.MapPath("~/images");
                        string targetPath = Path.Combine(targetFolder, file.ToString());

                        file.SaveAs(targetPath);    
                    }
                    error.Text = "File Uploaded";
                    error.Visible = true;

                }
                else
                {
                    error.Text = "no files to upload";
                    error.Visible = true;
                    return;
                }
            }
            catch(Exception ex)
            {
                error.Text = ex.ToString();
                error.Visible = true;
            }
        }

     

    This code works. And stores a file on the correct folder. The problem is that instead of it being a png like I uploaded its a 'Telerik.Web.UI.AsyncUploadedFile'

  2. Ivan Danchev
    Admin
    Ivan Danchev avatar
    835 posts

    Posted 29 Feb Link to this post

    Hello Joshua,

    In order to save the file and keep its name and extension you will have to change the following line in the UploadFiles_Click() handler:
    string targetPath = Path.Combine(targetFolder, file.ToString());

    to:
    string targetPath = Path.Combine(targetFolder, file.FileName);

    More information on how the files can be manipulated on the server side is available in the following article.

    Regards,
    Ivan Danchev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top