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

File doesn't upload using RadAsyncUpload in a RadWindow

4 Answers 112 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Troy
Top achievements
Rank 1
Troy asked on 25 Oct 2013, 06:23 PM

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?

 

 

 

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 28 Oct 2013, 05:02 AM
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.
0
Troy
Top achievements
Rank 1
answered on 28 Oct 2013, 11:59 AM
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?
0
Princy
Top achievements
Rank 2
answered on 29 Oct 2013, 02:44 AM
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.
0
Troy
Top achievements
Rank 1
answered on 29 Oct 2013, 12:03 PM
Yes, that will work.  I was missing creating the control in the Page_Init.  Thank you for your help!
Tags
General Discussions
Asked by
Troy
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Troy
Top achievements
Rank 1
Share this question
or