ASP.NET 2.0檔案上傳控制項

來源:互聯網
上載者:User


在ASP.NET 1.0/1.1,您可以上傳檔案使用的HTML檔案上傳伺服器的控制。這種控制使一個<input type="file">網頁上的內容,使終端使用者將檔案上傳到伺服器。要使用該檔案,但是,你必須要做出的修改夫婦到頁。例如,您都必須添加字元編碼=“多重/表單資料”頁面的<form>元素。

ASP.NET 2.0中引入了一個新檔案上傳伺服器控制項,使得檔案上傳到伺服器的過程更加簡單。當給一個頁面的能力,上傳檔案,您只需包括新<asp:FileUpload>控制和ASP.NET兼顧了其餘的,包括加入enctype屬性的頁面的<form>元素。

上傳檔案使用FileUpload控制項
該檔案後,上傳到伺服器,您還可以把握上傳檔案的屬性,要麼顯示給終端使用者或使用您的網頁的程式碼後置自己這些價值觀。清單1展示了一個使用新FileUpload控制項的例子。該網頁包含一個FileUpload控制項,以及一個按鈕和一個Label控制項。


VB

<%@ Page Language="VB"%>
<script runat="server">
   Protected Sub
   Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If FileUpload1.HasFile Then
            Try
                FileUpload1.SaveAs("C:Uploads" & _
                   FileUpload1.FileName)
                Label1.Text = "File name: " & _
                   FileUpload1.PostedFile.FileName & "<br>" & _
                   "File Size: " & _
                   FileUpload1.PostedFile.ContentLength & " kb<br>" & _
                   "Content type: " & _
                   FileUpload1.PostedFile.ContentType
            Catch ex As Exception
                Label1.Text = "ERROR: " & ex.Message.ToString()
            End Try
        Else
            Label1.Text = "You have not specified a file."
        End If
   End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>FileUpload Server Control</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <p>
        <asp:Button ID="Button1" runat="server" Text="Upload"
         OnClick="Button1_Click" /></p>
        <p>
        <asp:Label ID="Label1" runat="server"></asp:Label></p>
    </form>
</body>
</html>C#

<%@ Page Language="C#"%>
<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
           try {
              FileUpload1.SaveAs("C:Uploads" + FileUpload1.FileName);
              Label1.Text = "File name: " +
                   FileUpload1.PostedFile.FileName + "<br>" +
                   FileUpload1.PostedFile.ContentLength + " kb<br>" +
                   "Content type: " +
                   FileUpload1.PostedFile.ContentType; 
           }
            catch (Exception ex) {
              Label1.Text = "ERROR: " + ex.Message.ToString();
            }
        else
        {
           Label1.Text = "You have not specified a file.";
        }
    }
</script>

從這個例子可以看到,整個過程非常簡單。頁面上的一個按鈕啟動上傳過程。 FileUpload控制項本身並沒有主動上傳過程。你必須主動通過其他如Button_Click事件了。

當編譯和運行此網頁,您可能會注意到在網頁產生的原始碼幾件事。一個產生的原始碼樣本是這裡提出:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1"><title>
 FileUpload Server Control
</title></head>
<body>
    <form name="form1" method="post" action="FileUpload.aspx" id="form1"
     enctype="multipart/form-data">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
 value="/wEPDwUKMTI3ODM5MzQ0Mg9kFgICAw8WAh4HZW5jdHlwZQUTbXVsdGlwYXJ0L2Zvcm
 0tZGF0YWRkrSpgAFaEKed5+5/8+zKglFfVLCE=" />
</div>
   <input type="file" name="FileUpload1" id="FileUpload1" />
   <p>
   <input type="submit" name="Button1" value="Upload" id="Button1" /></p>
   <p>
   <span id="Label1"></span></p>

<div>
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION"
 value="/wEWAgL1wLWICAKM54rGBqfR8MhZIDWVowox+TUvybG5Xj0y" />
</div></form>
</body>
</html>

從這個例子可以看到,整個過程非常簡單。頁面上的一個按鈕啟動上傳過程。 FileUpload控制項本身並沒有主動上傳過程。你必須主動通過其他如Button_Click事件了。

當編譯和運行此網頁,您可能會注意到在網頁產生的原始碼幾件事。一個產生的原始碼樣本是這裡提出:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.