File doesn't upload using RadAsyncUpload in a RadWindow

5 posts, 0 answers
  1. Troy
    Troy avatar
    9 posts
    Member since:
    Feb 2013

    Posted 25 Oct 2013 Link to this post

    I have a RadAsyncUpload control in a RadWindow, but can't get it to save the file.  I previously had the same upload control dynamically added on the page (outside the window) and it was working fine.  Can someone help me with this?

     

    First of all, I have a RadWindow defined:

    <telerik:RadWindow ID="winUploadImage" runat="server" VisibleOnPageLoad="false" OnClientShow="windowLoad"
        VisibleStatusbar="false" AutoSize="false" Width="451" Height="275">
        <ContentTemplate>
        </ContentTemplate>
    </telerik:RadWindow>

     

     

    Next, I have a dynamically created RadAsyncUpload Control that is created and added to the window:

    protected void BtnUpload_Click(object sender, EventArgs e)
        {
            winUploadImage.ContentContainer.Controls.Add(new LiteralControl("<div class=\"quotebox\"><table class=\"quotetext\"><tr><td colspan=\"2\" class=\"quotetext\">"
                   + "</td></tr><tr><td class=\"quotetext\">Do you want to use a previously uploaded image?" + "</td>"
                   + "</tr>"
                   + "</table></div>"
                   ));
     
            RadButton clickedButton = new RadButton();
            clickedButton = (RadButton)sender;
     
            RadAsyncUpload upload = new RadAsyncUpload();
            upload.ID = "rauChoice";
            upload.Attributes.Add("runat", "server");
            upload.Attributes.Add("type", clickedButton.Attributes["type"]);
            upload.Attributes.Add("subtype", clickedButton.Attributes["subtype"]);
            //upload.Height = Unit.Parse("86px");
            //upload.Width = Unit.Parse("1px");
            upload.TargetFolder = "~/App_Images/uploads";
            upload.TemporaryFolder = "~/App_Data/RadUploadTemp";
            upload.FileUploaded += new FileUploadedEventHandler(ChangeFilename);
            upload.OnClientFileUploading = "uploading_file";
            upload.MaxFileInputsCount = 1;
            upload.MaxFileSize = 5000000; // 5 MB
            winUploadImage.ContentContainer.Controls.Add(imgAdd);
            winUploadImage.ContentContainer.Controls.Add(upload);
     
            winUploadImage.Modal = false;
            winUploadImage.Behaviors = Telerik.Web.UI.WindowBehaviors.Close;
            //winUploadImage.Top = 650;
            //winUploadImage.Left = 825;
            winUploadImage.OffsetElementID = divOptions.ClientID;
            winUploadImage.Top = 40;
            winUploadImage.Left = -25;
            winUploadImage.VisibleOnPageLoad = true;
        }


    When the files is uploaded, it should call this function

    protected void ChangeFilename(object sender, FileUploadedEventArgs e)
        {
            try
            {
                RadAsyncUpload uploadMod = new RadAsyncUpload();
                uploadMod = (RadAsyncUpload)sender;
                string targetFolder = uploadMod.TargetFolder;
     
                string newName =
                          uploadMod.Attributes["type"] + "!"
                        + uploadMod.Attributes["subtype"] + "!"
                        + DateTime.Now.ToString("yyyyMMddHHmmssfff")
                        ;
     
                Session[uploadMod.ID] = newName + e.File.GetExtension();
     
                e.File.SaveAs(Path.Combine(Server.MapPath(targetFolder), newName + e.File.GetExtension()));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


    When a button inside the window is pressed, the page posts back, but the file is not uploaded (it is not saved in the target folder and the ChangeFilename funtion is not called).

    How can I get the file to upload correctly?

     

     

     

  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 28 Oct 2013 Link to this post

    Hi Troy,

    Please have a look into the complete code I tried which works fine at my end.

    ASPX:
    <telerik:RadButton ID="RadButton1" runat="server" Text="Show Window" OnClick="RadButton1_Click">
    </telerik:RadButton>
    <telerik:RadWindow ID="winUploadImage" runat="server" VisibleOnPageLoad="false" VisibleStatusbar="false"
        AutoSize="false" Width="451" Height="275">
        <ContentTemplate>
        </ContentTemplate>
    </telerik:RadWindow>

    C#:
    protected void Page_Init(object sender, EventArgs e)
    {
        RadButton demo = new RadButton();
        demo.ID = "demo";
        demo.Text = "Uplaod";
        demo.Visible = false;
        form1.Controls.Add(demo);
     
        RadAsyncUpload upload = new RadAsyncUpload();
        upload.ID = "rauChoice";
        upload.Attributes.Add("runat", "server");
        upload.TargetFolder = "uploads";
        upload.TemporaryFolder = "~/App_Data/RadUploadTemp";
        upload.FileUploaded += new FileUploadedEventHandler(ChangeFilename);
        upload.MaxFileInputsCount = 1;
        upload.MaxFileSize = 5000000;
        winUploadImage.ContentContainer.Controls.Add(upload);
        winUploadImage.Modal = false;
        winUploadImage.Behaviors = Telerik.Web.UI.WindowBehaviors.Close;
    }
    protected void RadButton1_Click(object sender, EventArgs e)
    {
        winUploadImage.ContentContainer.Controls.Add(new LiteralControl("<div class=\"quotebox\"><table class=\"quotetext\"><tr><td colspan=\"2\" class=\"quotetext\">"
                + "</td></tr><tr><td class=\"quotetext\">Do you want to use a previously uploaded image?" + "</td>"
                + "</tr>"
                + "</table></div>"
                ));
        RadButton btn = (RadButton)form1.FindControl("demo");
        btn.Visible = true;
        winUploadImage.Top = 40;
        winUploadImage.Left = 85;
        winUploadImage.VisibleOnPageLoad = true;
     
    }
    protected void ChangeFilename(object sender, FileUploadedEventArgs e)
    {
        try
        {
            RadAsyncUpload uploadMod = new RadAsyncUpload();
            uploadMod = (RadAsyncUpload)sender;
            string targetFolder = uploadMod.TargetFolder;
     
            string newName =
                        uploadMod.Attributes["type"] + "!"
                    + uploadMod.Attributes["subtype"] + "!"
                    + DateTime.Now.ToString("yyyyMMddHHmmssfff")
                    ;
     
            Session[uploadMod.ID] = newName + e.File.GetExtension();
            e.File.SaveAs(Path.Combine(Server.MapPath(targetFolder), newName + e.File.GetExtension()));
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    Hope this will helps you.
    Thanks,
    Princy.
  3. Troy
    Troy avatar
    9 posts
    Member since:
    Feb 2013

    Posted 28 Oct 2013 Link to this post

    Thanks for the reply, but it won't work in my case.  I can't define the button in the .aspx page; it has to be defined dynamically in the code behind.  In this case, I have a variable number of buttons on the page; a button is created for each item from a database so I have to create the buttons dynamically.  Can you help me figure out how to get this to work when dynamically creating each button?
  4. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 28 Oct 2013 Link to this post

    Hi Troy,

    Please have a look into the following code snippet I tried.

    ASPX:
    <telerik:RadWindow ID="winUploadImage" runat="server" VisibleOnPageLoad="false" VisibleStatusbar="false"
        AutoSize="false" Width="451" Height="275">
        <ContentTemplate>
        </ContentTemplate>
    </telerik:RadWindow>

    C#:
    protected void Page_Init(object sender, EventArgs e)
    {
        //dynamically created button for show the RadWindow
        RadButton showwin = new RadButton();
        showwin.ID = "showwin";
        showwin.Text = "Show Window";
        showwin.Click += new EventHandler(showwin_Click);
        form1.Controls.Add(showwin);
     
        RadButton demo = new RadButton();
        demo.ID = "demo";
        demo.Text = "Uplaod";
        demo.Visible = false;
        form1.Controls.Add(demo);
     
        //dynamically created  radasyncupload
        RadAsyncUpload upload = new RadAsyncUpload();
        upload.ID = "rauChoice";
        upload.Attributes.Add("runat", "server");
        upload.TargetFolder = "uploads";
        upload.TemporaryFolder = "~/App_Data/RadUploadTemp";
        upload.FileUploaded += new FileUploadedEventHandler(ChangeFilename);
        upload.MaxFileInputsCount = 1;
        upload.MaxFileSize = 5000000;
        winUploadImage.ContentContainer.Controls.Add(upload);
        winUploadImage.Modal = false;
        winUploadImage.Behaviors = Telerik.Web.UI.WindowBehaviors.Close;
    }
     
    void showwin_Click(object sender, EventArgs e)
    {
        //click event of dynamically created button for showing radwindow
        winUploadImage.ContentContainer.Controls.Add(new LiteralControl("<div class=\"quotebox\"><table class=\"quotetext\"><tr><td colspan=\"2\" class=\"quotetext\">"
                + "</td></tr><tr><td class=\"quotetext\">Do you want to use a previously uploaded image?" + "</td>"
                + "</tr>"
                + "</table></div>"
                ));
        RadButton btn = (RadButton)form1.FindControl("demo");
        btn.Visible = true;
        winUploadImage.Top = 40;
        winUploadImage.Left = 85;
        winUploadImage.VisibleOnPageLoad = true;
    }

    Hope this will helps you.
    Thanks,
    Princy.
  5. Troy
    Troy avatar
    9 posts
    Member since:
    Feb 2013

    Posted 29 Oct 2013 Link to this post

    Yes, that will work.  I was missing creating the control in the Page_Init.  Thank you for your help!
Back to Top