Onlineatoz.net | ToolBox HTML Control HTMLFile

ToolBox HTML Control HTMLFile

HTML File Web Server Control

In ASP.NET environment,This article describes you how to work with HTML File Control.In HTML environment, File is an object that represents a FileUpload in an HTML form. The FileUpload control in a HTML form contains all instance of an <input type="file"> tag.

The HtmlInputFile control can be used to uploading binary or text files from a browser client to the server. which was implemented and works with Microsoft Internet Explorer version 3.02 or Higher. HtmlInputFile controls requires the full path of the uploading file.for example ,"d:\Photos\img1.jpg" in the text box of the control.

There are several functions available to access the HTML File.some of its funtions are as follows,

  • document.getElementById()
  • document.getElementByName()
File Objects Properties
  • id -Sets or returns the id of a File
  • form -Returns a reference to the form that contains the File
  • accessKey -Sets or returns the keyboard key to access a File
  • alt -Sets or returns an alternate text to display if a browser does not support Files
  • disabled -Sets or returns whether or not a File should be disabled
  • tabIndex -Sets or returns the tab order for a File
  • name -Sets or returns the name of a File
  • type -Returns the type of form element a File is
  • defaultvalue - Sets or returns the initial value of the FileUpload object
  • value -Sets or returns the text that is displayed on the File
  • title -Sets or returns an element's advisory title
  • lang -Sets or returns the language code for an element
  • dir -Sets or returns the direction of text
  • className -Sets or returns the class attribute of an element
File Objects Methods
  • blur() -Performs focus when leave from the control
  • click() -Simulates a mouse-click on a File
  • focus() -Performs focus when enter into the control

The following code snippets explains you the HTML File in the web form.

in .aspx.cs page,

void btn1_Click(object Source, EventArgs e)
  {

    if (txt1.Value == "")
    {
      Span1.InnerHtml = "file name required";
      return;
    }

    if (File1.PostedFile.ContentLength > 0)
    {
      try
      {
        File1.PostedFile.SaveAs("d:\\apppath\\" + txt1.Value);
        Span1.InnerHtml = "File uploaded successfully to c:\\temp\\"
         + txt1.Value + " on the Web server.";
      }
      catch (Exception ex)
      {
        Span1.InnerHtml = "Error saving file d:\\apppath\\" + txt1.Value + "
" + ex.ToString() + "."; } } }

in .aspx page,

<body> 
    <h3>Example</h3>
     <form id="Form2" enctype="multipart/form-data" 
          runat="server">
       Select File to Upload: 
       <input id="File1" 
              type="file" 
              runat="server">
      <p>
       <input id="txt1" 
              type="text" 
              runat="server">
       </p>
       <p>
       <span id=Span1 
             style="font: 8pt verdana;" 
             runat="server" />
       </p>
       <p>
       <input type=button 
              id="btn1" 
              value="Upload" 
              onserverclick="btn1_Click" 
             runat="server">
       </p>
   </form>
 </body>
 
Related Links

Posted by: Admin
Posted on: 9/12/2011 at 8:19 PM
Categories: Asp.net
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed