HtmlInputFile.Accept Property

Definition

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

C#
public string Accept { get; set; }

Property Value

The comma-separated list of MIME encodings.

Examples

The following code example demonstrates how to use the Accept property to restrict the user from selecting any file other than an image file. For this example to work properly, you need to create a directory called Temp on your computer's drive C. Because support for the Accept property is browser-dependent, a server-side check is performed to ensure that only images are uploaded. An exception is thrown if the uploaded file can not be read as an Image type.

ASP.NET (C#)
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    void Button1_Click(object Source, EventArgs e)
    {

        // Check to see if a file was selected.
        if (Text1.Value == "")
        {
            Span1.InnerHtml = "Error: You must enter a file name.";
            return;
        }

        // Save the file.
        if (File1.PostedFile.ContentLength > 0)
        {
            try
            {
                try
                {
                    using (System.Drawing.Image input =
                        System.Drawing.Image.FromStream(File1.PostedFile.InputStream))
                    {
                        File1.PostedFile.SaveAs("c:\\temp\\" + 
                            Server.HtmlEncode(Text1.Value));
                        Span1.InnerHtml = "File uploaded successfully to <b>c:\\temp\\" +
                            Server.HtmlEncode(Text1.Value) + 
                            "</b> on the Web server.";
                    }
                }
                catch
                {
                    throw new Exception("Not a valid image file.");
                }
            }
            catch (Exception exc)
            {
                Span1.InnerHtml = "Error saving file <b>c:\\temp\\" +
                                   Server.HtmlEncode(Text1.Value) + 
                                   "</b>. " + exc.Message;
            }
        }
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>HmtlInputFile Example</title>
</head>
<body>
    <form id="Form1" enctype="multipart/form-data" 
          runat="server">
    <div>  
       Select File to Upload: 
       <input id="File1" 
              type="file"              
              accept="image/*"
              runat="server"/>
       <p>
       Save as file name (no path): 
       <input id="Text1" 
              type="text" 
              runat="server"/>
       </p>
       <p>
       <span id="Span1" 
             style="font: 8pt verdana;" 
             runat="server" />
 
       </p>
       <p>
       <input type="button" 
              id="Button1" 
              value="Upload" 
              onserverclick="Button1_Click" 
              runat="server" />
       </p>
    </div>
    </form>
</body>
</html>

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 your browser to determine whether it supports this property. It is recommended that you use server-side code to ensure that the file is of the expected type.

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1