ASP.NET, HtmlInputField, CodeBehind

Junoti
Junoti used Ask the Experts™
on
Can anyone help me out here? I'm trying to determine why I can't get this code to work. The only reason I use the <input type=file> tag is so that a user has access to the browse button. Once the file file is selected, I'll be grabbing the complete path to the file in order to process it.

In my .aspx I have the following two lines in a form (all fields are set to runat=server):

<input type="file" id="imagefile" size="30" runat="server"/>
<input type="file" id="thumbfile" size="30" runat="server"/>

In my .cs file (codebehind) I have the following lines to capture the values:

(in class definition)
protected HtmlInputFile imagefile;
protected HtmlInputFile thumbfile;


(in Page_Load event)
private void Page_Load(object sender, System.EventArgs e)
{
      if(this.IsPostBack)
      {
            Response.Write(imagefile.PostedFile.FileName + "<br/>" + thumbfile.PostedFile.FileName);
      }
}


If I leave the fields blank, the page posts back to itself fine. However, if I select a file for both pages, it doesn't seem to work. For some reason it takes me back to 404 error page (page cannot be displayed). The URL in my address bar never changes. Any insights?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Try this it works no probs:


                                <form enctype="multipart/form-data" runat="server" ID="Form1">
                  File: <INPUT id=imagefile type=file name=myFile runat="server">
                  File: <input id="thumbfile" type="file" runat="server" NAME="myFile">
                  <asp:label id="lblMsg" runat="server" />
                  <input type="button" value="Upload" runat="server" ID="Button1" NAME="Button1">
            </form>

                                protected System.Web.UI.WebControls.Label lblMsg;
            protected System.Web.UI.HtmlControls.HtmlInputFile thumbfile;
            protected System.Web.UI.HtmlControls.HtmlInputFile imagefile;
            protected System.Web.UI.HtmlControls.HtmlInputFile File1;
            protected System.Web.UI.HtmlControls.HtmlInputFile myFile;
            protected System.Web.UI.HtmlControls.HtmlInputButton Button1;

                                private void Button1_ServerClick(object sender, System.EventArgs e)
            {
                  Response.Write(thumbfile.PostedFile.FileName + imagefile.PostedFile.FileName);
            }

Regards
Chris

Commented:
I should mention if you paste the .cs file stuff in first it will create most of the html automatically, you just have to then go in and change the <form enctype=...

Chris
Michel SakrRounded IT Consultant / Projects manager / Cloud Consultant / IT Planning

Commented:
I'll be grabbing the complete path to the file in order to process it <-- you can't have this.. you can get the file instead that gets uploaded to the webserver..
what you might have also is the 4 Mb file size restriction in asp.net ..

simply change the default upload limit in web.config file.

<?xml version="1.0" encoding="UTF-8" ?>
 <configuration>
   <configSections>
    <system.web>
     <httpRuntime
      executionTimeout="90"
      maxRequestLength="10240" <!-- 10Mb -->
      useFullyQualifiedRedirectUrl="false"
     />
    </system.web>
   </configSections>
 </configuration>
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Michel SakrRounded IT Consultant / Projects manager / Cloud Consultant / IT Planning

Commented:

Author

Commented:
I've seen that convention used while researching it on the web, but it doesn't make sense why it wouldn't work in the Page_Load event. Thanks for the help.

Commented:
Sorry it was just force of habit to put the event on the button click it still works fine as...

            private void Page_Load(object sender, System.EventArgs e)
            {

                  if(this.IsPostBack)
                  {
                  Response.Write(thumbfile.PostedFile.FileName + imagefile.PostedFile.FileName);
                  }
            }
Regards
Chris

Author

Commented:
What is the preferred method (standard) for handling form processing? On the Page_Load event or button click? It doesn't seem to work for me using the Page_Load event, but button click works fine.

Commented:
I tend to use the button click now (since the move from asp to asp.net), it makes a more organised way to break up your code rather than using lots of if or select statments in the page_load event. Code i posted for Page_Load works for me, make sure you take the Response.Write line out of the button click event.

Regards
Chris

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial