HtmlInputFile.Accept 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定一個逗號分隔的 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
屬性值
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/*」。
備註
此屬性的支援依瀏覽器而異。 請檢查瀏覽器是否支援此特性。 建議使用伺服器端程式碼,以確保檔案的類型符合預期。