HtmlInputFile.Accept 属性

定义

获取或设置用逗号分隔的 MIME 编码列表,该列表用于约束用户可以选择的文件类型。

public:
 property System::String ^ Accept { System::String ^ get(); void set(System::String ^ value); };
public string Accept { get; set; }
member this.Accept : string with get, set
Public Property Accept As String

属性值

String

用逗号分隔的 MIME 编码列表。

示例

下面的代码示例演示如何使用 Accept 属性来限制用户选择图像文件以外的任何文件。 若要使此示例正常工作,需要在计算机的驱动器 C 上创建一个调用 Temp 的目录。由于对属性的支持 Accept 依赖于浏览器,因此将执行服务器端检查以确保仅上传图像。 如果上传的文件无法作为 Image 类型读取,则会引发异常。

<%@ 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>
<%@ Page Language="VB" %>

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

<script runat="server">

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        ' Check to see if a file was selected.
        If (Text1.Value = "") Then
            Span1.InnerHtml = "Error: You must enter a file name."
            Return
        End If
        
        ' Save the file
        If (File1.PostedFile.ContentLength > 0) Then
            Try
                Try
                    Using input As System.Drawing.Image = _
                       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."
                        
                    End Using
                Catch
                    Throw New Exception("Not a valid image file.")
                End Try
            Catch exc As Exception
                Span1.InnerHtml = "Error saving file <b>c:\\temp\\" & _
                                   Server.HtmlEncode(Text1.Value) & _
                                   "</b>. " & exc.Message
            End Try
        End If
        
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>HtmlInputFile Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       Select File to Upload: 
       <input id="File1" 
              type="file"              
              accept="image/jpeg"
              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>

注解

使用此属性可指定可上传到服务器的文件类型。 例如,若要将所选内容限制为图像,请将此属性设置为“image/*”。

备注

对此属性的支持依赖于浏览器。 检查浏览器以确定它是否支持此属性。 建议使用服务器端代码来确保文件为预期类型。

适用于