This documentation is archived and is not being maintained.

HtmlInputFile.Accept Property

Gets or sets a comma-separated list of MIME encodings used to constrain the file types the user can select.

[Visual Basic]
Public Property Accept As String
[C#]
public string Accept {get; set;}
[C++]
public: __property String* get_Accept();
public: __property void set_Accept(String*);
[JScript]
public function get Accept() : String;
public function set Accept(String);

Property Value

The comma-separated list of MIME encodings.

Remarks

Use this property to specify the file type that can be uploaded to the server. For example, to restrict the selection to images, set this property to "image/*".

Note   Support for this property is browser dependent. Check with your browser to determine if it supports this property.

Example

[Visual Basic, C#, JScript] The following example demonstrates how to use the Accept property to restrict the user from selecting any file other than an image file.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
 <head>
 
    <script language="VB" runat="server">
    Sub Button1_Click(Source As Object, e As EventArgs)
        
        If Text1.Value = "" Then
            Span1.InnerHtml = "Error: you must enter a file name"
            Return
        End If
        
        If Not (File1.PostedFile Is Nothing) Then
            Try
                File1.PostedFile.SaveAs(("c:\temp\" & Text1.Value))
                Span1.InnerHtml = "File uploaded successfully to <b>c:\temp\" & _ 
                                      Text1.Value & "</b> on the Web server"
            Catch exc As Exception
                Span1.InnerHtml = "Error saving file <b>c:\temp\" & _ 
                                      Text1.Value & "</b><br>" & exc.ToString()
            End Try
        End If
    End Sub 'Button1_Click
  </script>
 
 </head>
 <body>
 
    <h3>HtmlInputFile Sample</h3>
 
    <form enctype="multipart/form-data" runat="server">
 
       Select File to Upload: 
       <input id="File1" 
              type="file"
              accept="image/*"
              runat="server"/>
 
       <p>
       Save as filename (no path): 
       <input id="Text1" 
              type="text" 
              runat="server"/>
 
       <p>
       <span id=Span1 
             style="font: 8pt verdana;" 
             runat="server" />
 
       <p>
       <input type=button 
              id="Button1" 
              value="Upload" 
              OnServerClick="Button1_Click" 
              runat="server">
 
    </form>
 
 </body>
 </html>
   

[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
 <head>
 
    <script language="C#" runat="server">
 
       void Button1_Click(object Source, EventArgs e) 
       {
 
          if (Text1.Value == "") 
          {
             Span1.InnerHtml = "Error: you must enter a file name";
             return;
          }
 
          if (File1.PostedFile != null) 
          {
             try 
             {
                File1.PostedFile.SaveAs("c:\\temp\\"+Text1.Value);
                Span1.InnerHtml = "File uploaded successfully to <b>c:\\temp\\" + 
                                  Text1.Value + "</b> on the Web server";
             }
             catch (Exception exc) 
             {
                Span1.InnerHtml = "Error saving file <b>c:\\temp\\" + 
                                  Text1.Value + "</b><br>" + exc.ToString();
             }
          }
       }
 
    </script>
 
 </head>
 <body>
 
    <h3>HtmlInputFile Sample</h3>
 
    <form enctype="multipart/form-data" runat="server">
 
       Select File to Upload: 
       <input id="File1" 
              type="file"
              accept="image/*"
              runat="server"/>
 
       <p>
       Save as filename (no path): 
       <input id="Text1" 
              type="text" 
              runat="server"/>
 
       <p>
       <span id=Span1 
             style="font: 8pt verdana;" 
             runat="server" />
 
       <p>
       <input type=button 
              id="Button1" 
              value="Upload" 
              OnServerClick="Button1_Click" 
              runat="server">
 
    </form>
 
 </body>
 </html>
   

[JScript] 
<%@ Page Language="JScript" AutoEventWireup="True" %>

<html>
 <head>
 
    <script language="JSCRIPT" runat="server">
    function Button1_Click(source : Object, e : EventArgs){
        
        if(Text1.Value == ""){
            Span1.InnerHtml = "Error: you must enter a file name"
            return
        }
        
        if(File1.PostedFile != null){
            try{
                File1.PostedFile.SaveAs(("c:\\temp\\" + Text1.Value))
                Span1.InnerHtml = "File uploaded successfully to <b>c:\\temp\\"
                                + Text1.Value + "</b> on the Web server"
            }catch(exc : Exception){
                Span1.InnerHtml = "Error saving file <b>c:\\temp\\"
                                + Text1.Value + "</b><br>" + exc.ToString()
            }
        }
    }
  </script>
 
 </head>
 <body>
 
    <h3>HtmlInputFile Sample</h3>
 
    <form enctype="multipart/form-data" runat="server">
 
       Select File to Upload: 
       <input id="File1" 
              type="file"
              accept="image/*"
              runat="server"/>
 
       <p>
       Save as filename (no path): 
       <input id="Text1" 
              type="text" 
              runat="server"/>
 
       <p>
       <span id=Span1 
             style="font: 8pt verdana;" 
             runat="server" />
 
       <p>
       <input type=button 
              id="Button1" 
              value="Upload" 
              OnServerClick="Button1_Click" 
              runat="server">
 
    </form>
 
 </body>
 </html>
   

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

HtmlInputFile Class | HtmlInputFile Members | System.Web.UI.HtmlControls Namespace

Show: