-
[CLOSED] file uploader
Hi,
I am using your example for uploading a file, but when it comes to the "if (this.FileUploadField1.HasFile)" it is false when I am using a tool from the window to save. for testing purposes I tried using a direct event from the fileuploader and it works from there. But I need to upload the file during the save event, not before. please advise.
Code:
<%@ Control Language="C#" %>
<ext:DesktopModuleProxy ID="DesktopModuleProxy1" runat="server">
<Module ModuleID="create-test">
<Shortcut Name="test" IconCls="x-edits-shortcut" SortIndex="1" />
<Launcher Text="test" Icon="Table" />
<Window>
<ext:Window ID="Window1" runat="server" Width="500" Height="200">
<Tools>
<ext:Tool Type="Save" ToolTip="Save information" >
<DirectEvents>
<Click OnEvent="UploadClick" /> <%--from here it does not work!!!--%>
</DirectEvents>
</ext:Tool>
</Tools>
<Items>
<ext:FormPanel ID="FormPanel1" runat="server">
<Items>
<ext:FileUploadField runat="server" FieldLabel="Company Logo" EmptyText="Select an image" ButtonText="" ID="FileUploadField1" Icon="ImageAdd" >
<LeftButtons>
<ext:Button runat="server">
<DirectEvents>
<Click OnEvent="UploadClick" /> <%--from here it works--%>
</DirectEvents>
</ext:Button>
</LeftButtons>
</ext:FileUploadField>
</Items>
</ext:FormPanel>
</Items>
</ext:Window>
</Window>
</Module>
</ext:DesktopModuleProxy>
and in the code CodeBehind
Code:
public void UploadClick(object sender, DirectEventArgs e)
{
string tpl = "Uploaded file: {0}<br/>Size: {1} bytes";
if (this.FileUploadLogo.HasFile)
{
//FileUploadLogo.PostedFile.SaveAs(
X.Msg.Show(new MessageBoxConfig
{
Buttons = MessageBox.Button.OK,
Icon = MessageBox.Icon.INFO,
Title = "Success",
Message = string.Format(tpl, this.FileUploadLogo.PostedFile.FileName, this.FileUploadLogo.PostedFile.ContentLength)
});
}
else
{
X.Msg.Show(new MessageBoxConfig
{
Buttons = MessageBox.Button.OK,
Icon = MessageBox.Icon.ERROR,
Title = "Fail",
Message = "No file uploaded"
});
}
}
-
Hello @stefanos!
Can you reproduce the issue in an standalone example (no desktop nor controls) and provide us the code, so we can reproduce it on our side?
-
Code:
<%@ Page Language="C#" CodeBehind="test.aspx.cs" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<ext:ResourceManager ID="ResourceManager1" runat="server" />
<ext:Window ID="Window1" runat="server" Width="500" Height="200">
<Tools>
<ext:Tool Type="Save" ToolTip="Save information" >
<DirectEvents>
<Click OnEvent="UploadClick" /> <%--from here it does not work!!!--%>
</DirectEvents>
</ext:Tool>
</Tools>
<Items>
<ext:FormPanel ID="FormPanel1" runat="server">
<Items>
<ext:FileUploadField runat="server" FieldLabel="Company Logo" EmptyText="Select an image" ButtonText="" ID="FileUploadField1" Icon="ImageAdd" >
<LeftButtons>
<ext:Button ID="Button1" runat="server">
<DirectEvents>
<Click OnEvent="UploadClick" /> <%--from here it works--%>
</DirectEvents>
</ext:Button>
</LeftButtons>
</ext:FileUploadField>
</Items>
</ext:FormPanel>
</Items>
</ext:Window>
</body>
</html>
codebehind
Code:
public void UploadClick(object sender, DirectEventArgs e)
{
string tpl = "Uploaded file: {0}<br/>Size: {1} bytes";
if (this.FileUploadField1.HasFile)
{
//FileUploadLogo.PostedFile.SaveAs(
X.Msg.Show(new MessageBoxConfig
{
Buttons = MessageBox.Button.OK,
Icon = MessageBox.Icon.INFO,
Title = "Success",
Message = string.Format(tpl, this.FileUploadField1.PostedFile.FileName, this.FileUploadField1.PostedFile.ContentLength)
});
}
else
{
X.Msg.Show(new MessageBoxConfig
{
Buttons = MessageBox.Button.OK,
Icon = MessageBox.Icon.ERROR,
Title = "Fail",
Message = "No file uploaded"
});
}
}
-
Hello @stefanos!
Thanks for the clear sample!
We are investigating this. Usually relying on page values from code behind is not safe, but in some common circumstances, we ensure data is submitted, which is the case of the buttons enclosed in the LeftButtons of the file upload field. As the other button is outside, it does not carry the component state to code behind.
I'm actually more surprised why this worked in the button by the upload file than why it didn't work when called from outside.
Anyway, until we have a better answer, here's a pragmatic idea to just make it work the way you could have it working:
Code:
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
public void UploadClick(object sender, DirectEventArgs e)
{
string tpl = "Uploaded file: {0}<br/>Size: {1} bytes";
if (this.FileUploadField1.HasFile)
{
//FileUploadLogo.PostedFile.SaveAs(
X.Msg.Show(new MessageBoxConfig
{
Buttons = MessageBox.Button.OK,
Icon = MessageBox.Icon.INFO,
Title = "Success",
Message = string.Format(tpl, this.FileUploadField1.PostedFile.FileName, this.FileUploadField1.PostedFile.ContentLength)
});
}
else
{
X.Msg.Show(new MessageBoxConfig
{
Buttons = MessageBox.Button.OK,
Icon = MessageBox.Icon.ERROR,
Title = "Fail",
Message = "No file uploaded"
});
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<ext:ResourceManager ID="ResourceManager1" runat="server" />
<ext:Window ID="Window1" runat="server" Width="500" Height="200">
<Tools>
<ext:Tool Type="Save" ToolTip="Save information" Handler="App.Button1.click();" />
</Tools>
<Items>
<ext:FormPanel ID="FormPanel1" runat="server">
<Items>
<ext:FileUploadField runat="server" FieldLabel="Company Logo" EmptyText="Select an image" ButtonText="" ID="FileUploadField1" Icon="ImageAdd" >
<LeftButtons>
<ext:Button ID="Button1" runat="server" Hidden="true">
<DirectEvents>
<Click OnEvent="UploadClick" /> <%--from here it works--%>
</DirectEvents>
</ext:Button>
</LeftButtons>
</ext:FileUploadField>
</Items>
</ext:FormPanel>
</Items>
</ext:Window>
</body>
</html>
Basically, it just hides the button that works and uses the outside button to send a 'click' stimulus to the button by the field, thus ensuring the context of the click sending component is maintained.
I hope this helps for the meanwhile!
-
thanks for that, it was very useful!
-
Hello,
A DirectEvent searches for a <form>
or a <ext:FormPanel>
for auto-submitting up the hierarchy starting from the control owning that DirectEvent.
So, in the case with the Button inside LeftButtons it can find the FormPanel. But there is no <form>
/<ext:FormPanel>
when it searches from the Tool and the FileUploadField is not auto-submitted.
You can either define <form runat="server">
on the page or set up this:
Code:
<Click OnEvent="UploadClick" FormID="FormPanel1" />
-
Thank you Daniil for this solution. This actualy solves also many other troubles I had with the user controls along with direct methods.