[CLOSED] file uploader

  1. #1

    [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.

    <%@ 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

     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"
                    });
                }
            }
    Last edited by Daniil; Sep 01, 2016 at 3:39 PM. Reason: [CLOSED]
  2. #2
    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?
    Fabrício Murta
    Developer & Support Expert
  3. #3
    <%@ 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
    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"
                    });
                }
            }
  4. #4
    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:

    <%@ 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!
    Fabrício Murta
    Developer & Support Expert
  5. #5
    thanks for that, it was very useful!
  6. #6
    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:
    <Click OnEvent="UploadClick" FormID="FormPanel1" />
  7. #7
    Thank you Daniil for this solution. This actualy solves also many other troubles I had with the user controls along with direct methods.

Similar Threads

  1. Replies: 4
    Last Post: Nov 22, 2013, 11:28 AM
  2. Replies: 3
    Last Post: Apr 08, 2013, 10:35 PM
  3. Replies: 2
    Last Post: Mar 07, 2013, 9:03 AM
  4. example about an uploader multi manager
    By tk.Mageztik in forum 2.x Help
    Replies: 1
    Last Post: Sep 13, 2012, 6:05 AM
  5. File uploader with the list of uploaded files
    By AlexMaslakov in forum 1.x Help
    Replies: 0
    Last Post: Aug 11, 2011, 11:56 AM

Tags for this Thread

Posting Permissions