[CLOSED] Please help me with a fileupload question

  1. #1

    [CLOSED] Please help me with a fileupload question

    I implemented my file upload dialog in a Window ->formPanel, using the example from example explorer. The only thing I did additional is to put below code in a window, When i click a button a window with fie upload dialog with come up. But when i hit save, if (this.FileUploadField1.HasFile) always returns nothing.

    What should I do to make it work?

    <ext:Window ID="WinAttach" runat="server" Title="Add an Attachment to Contract" Modal="true"
    Hidden="true" ShowOnLoad="false" AutoShow="false" Width="520" Height="160">
    <Items>
     <ext:FormPanel 
                ID="BasicForm" 
                runat="server"
                Width="500"
                Frame="true"
                Title="File Upload Form"
                AutoHeight="true"
                MonitorValid="true"
                PaddingSummary="10px 10px 0 10px"
                LabelWidth="50">                
                <Defaults>
                    <ext:Parameter Name="anchor" Value="95%" Mode="Value" />
                    <ext:Parameter Name="allowBlank" Value="false" Mode="Raw" />
                    <ext:Parameter Name="msgTarget" Value="side" Mode="Value" />
                </Defaults>
                <Items>
                    <ext:TextField ID="PhotoName" runat="server" FieldLabel="Name" />
                    <ext:FileUploadField 
                        ID="FileUploadField1" 
                        runat="server" 
                        EmptyText="Select an image"
                        FieldLabel="Photo"
                        ButtonText=""
                        Icon="ImageAdd">
                    </ext:FileUploadField>
                </Items>
                <Listeners>
                    <ClientValidation Handler="#{SaveButton}.setDisabled(!valid);" />
                </Listeners>
                <Buttons>
                    <ext:Button ID="SaveButton" runat="server" Text="Save">
                        <DirectEvents>
                            <Click 
                                OnEvent="UploadClick"
                                Before="if (!#{BasicForm}.getForm().isValid()) { return false; } 
                                    Ext.Msg.wait('Uploading your photo...', 'Uploading');"
                                    
                                Failure="Ext.Msg.show({ 
                                    title   : 'Error', 
                                    msg     : 'Error during uploading', 
                                    minWidth: 200, 
                                    modal   : true, 
                                    icon    : Ext.Msg.ERROR, 
                                    buttons : Ext.Msg.OK 
                                });">
                            </Click>
                        </DirectEvents>
                    </ext:Button>
                    <ext:Button runat="server" Text="Reset">
                        <Listeners>
                            <Click Handler="#{BasicForm}.getForm().reset();" />
                        </Listeners>
                    </ext:Button>
                </Buttons>
            </ext:FormPanel>
    </Items>
    </ext:Window>
     
    protected void UploadClick(object sender, DirectEventArgs e)
    {
    string tpl = "Uploaded file: {0}<br/>Size: {1} bytes";
    if (this.FileUploadField1.HasFile)
    {
    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"
    });
    }
    }
    Last edited by geoffrey.mcgill; Jul 22, 2010 at 6:09 PM. Reason: please use [code] tags
  2. #2
    Hi,

    Please set IsUpload="true" on the <Click> DirectEvent.

    Hope this helps.
    Geoffrey McGill
    Founder
  3. #3
    Thanks for the reply. I just tried adding isUpload="true" ,
    this.FileUploadField1.HasFile still returns false. is there anything else i could try?
  4. #4
    Hi,

    Your original code sample appears to work properly for me.

    Example

    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void UploadClick(object sender, DirectEventArgs e)
        {
            string tpl = "Uploaded file: {0}<br/>Size: {1} bytes";
    
            if (this.FileUploadField1.HasFile)
            {
                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>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Ext.NET Example</title>
    </head>
    <body>
    <form runat="server">
        <ext:ResourceManager runat="server" />
     
        <ext:Window 
            ID="WinAttach"
            runat="server"
            Title="Add an Attachment to Contract"
            Modal="true"
            Width="520"
            Height="160">
            <Items>
                <ext:FormPanel
                    ID="BasicForm"
                    runat="server"
                    Width="500"
                    Frame="true"
                    Title="File Upload Form"
                    PaddingSummary="10px 10px 0 10px"
                    LabelWidth="50">                
                    <Defaults>
                        <ext:Parameter Name="anchor" Value="95%" Mode="Value" />
                        <ext:Parameter Name="allowBlank" Value="false" Mode="Raw" />
                        <ext:Parameter Name="msgTarget" Value="side" Mode="Value" />
                    </Defaults>
                    <Items>
                        <ext:TextField ID="PhotoName" runat="server" FieldLabel="Name" />
                        <ext:FileUploadField
                            ID="FileUploadField1"
                            runat="server"
                            EmptyText="Select an image"
                            FieldLabel="Photo"
                            ButtonText=""
                            Icon="ImageAdd">
                        </ext:FileUploadField>
                    </Items>
                    <Listeners>
                        <ClientValidation Handler="#{SaveButton}.setDisabled(!valid);" />
                    </Listeners>
                    <Buttons>
                        <ext:Button ID="SaveButton" runat="server" Text="Save">
                            <DirectEvents>
                                <Click
                                    OnEvent="UploadClick"
                                    Before="if (!#{BasicForm}.getForm().isValid()) { return false; } 
                                        Ext.Msg.wait('Uploading your photo...', 'Uploading');"
                                         
                                    Failure="Ext.Msg.show({ 
                                        title   : 'Error', 
                                        msg     : 'Error during uploading', 
                                        minWidth: 200, 
                                        modal   : true, 
                                        icon    : Ext.Msg.ERROR, 
                                        buttons : Ext.Msg.OK 
                                    });">
                                </Click>
                            </DirectEvents>
                        </ext:Button>
                        <ext:Button runat="server" Text="Reset">
                            <Listeners>
                                <Click Handler="#{BasicForm}.getForm().reset();" />
                            </Listeners>
                        </ext:Button>
                    </Buttons>
                </ext:FormPanel>
            </Items>
        </ext:Window>
    </form>
    </body>
    </html>
    Geoffrey McGill
    Founder
  5. #5
    Thanks. I just looked at your code. I realized my form is missing.
    <form runat="server">
    </form> not sure how that happened
    I added it back. it worked.

    Thank you!!

  6. #6

    Issue in fileupload

    Hello,

    I have it under <form runat="server">
    but still I am not able to get the filename. It always shows false.

    Please help.

    Regards


    Quote Originally Posted by vali1993 View Post
    Thanks. I just looked at your code. I realized my form is missing.
    <form runat="server">
    </form> not sure how that happened
    I added it back. it worked.

    Thank you!!
  7. #7
    Hi @vbdotnetphp,

    Please provide a sample to reproduce.
  8. #8

    Fileupload question

    Hi,

    I have solved the problem.

    It was MVC code.

    The "View" code had the following :

    <form runat="server">
    
      <ext:FormPanel ID="FormPanel1" runat="server" Layout="Fit">
                <Items>
                    <ext:FileUploadField ID="fileupload" runat="server" Icon="PageExcel" ButtonText="Browse">                    
                    </ext:FileUploadField>
                    <ext:Button ID="Button1" runat="server" Text="Upload">
                        <DirectEvents>
                            <Click Url="../../Account/SponsorUploadFile/" IsUpload="true" CleanRequest="true" Method="POST" >
                            </Click>
                        </DirectEvents>
                    </ext:Button>               
                </Items>
            </ext:FormPanel>
    </form>
    and the corresponding Controller code was as follows :

    public AjaxResult SponsorUploadFile()
            {
                AjaxResult result = new AjaxResult();
                try
                {
                    HttpPostedFileBase file = Request.Files[0];                
    
                    result.IsUpload = true;
                    bool err = false;
                    if (!file.FileName.ToLower().Contains("jpg"))
                    {
                        if (!file.FileName.ToLower().Contains("gif"))
                        {
                            if (!file.FileName.ToLower().Contains("bmp"))
                            {
                                if (!file.FileName.ToLower().Contains("png"))
                                {
                                    err = true;
                                }
                            }
                        }
                    }
                    string s = Server.MapPath("");
                    if (err == false)
                        file.SaveAs(Server.MapPath("../../Content/Resources/SponsorLogos/s.jpg")); //just for test
    
                    if (err == true)
                        result.ErrorMessage = "Please enter valid image format";
                }
                catch (Exception ex)
                {
                    result.ErrorMessage = ex.Message.ToString();
                }
                return result;
                
            }
    In this code, please see this first line
    HttpPostedFileBase file = Request.Files[0];
    The Request.Files[0] always was null. I found out why.
    The reason was there were 2 arguments being passed to Request. And the last argument had the file details.

    So what I did was , I just changed the index from 0 to 1. And doing that it worked!!!
    So now it is

    HttpPostedFileBase file = Request.Files[1];

    Regards
    Last edited by geoffrey.mcgill; Oct 13, 2012 at 6:49 AM.
  9. #9
    I think it can depend on a browser and, maybe, on OS.

    I just tested with Windows 7 and IE9, FireFox and Chrome. It worked well with
    Request.Files[0]
    I think Google can answer this issue.

Similar Threads

  1. [CLOSED] [MVC] FileUpload in MVC
    By UnifyEducation in forum 2.x Legacy Premium Help
    Replies: 6
    Last Post: Jan 27, 2015, 4:03 PM
  2. [CLOSED] [#92] FileUpload
    By Timothy in forum 2.x Legacy Premium Help
    Replies: 8
    Last Post: Jan 16, 2013, 3:44 AM
  3. [CLOSED] [1.0] Please help me with fileupload filepath
    By vali1993 in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Aug 10, 2010, 2:33 PM
  4. [CLOSED] fileupload question
    By idrissb in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Sep 02, 2009, 1:46 PM
  5. bug in fileupload
    By pablisho in forum 1.x Help
    Replies: 0
    Last Post: Jul 01, 2009, 10:55 AM

Posting Permissions