• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • 檔案上傳處理

    發表于:2007-07-14來源:作者:點擊數: 標簽:
    使用ASP.NET 制作一個可以存放Client 端檔案的網頁相當容易,因為ASP.NET 里就有提供我們將Client 端檔案傳至Server 端的對象,這個對象就是HtmlInputFile 對象。HtmlInputFile 對象必須存在窗體中,而且窗體Form 標注中必須加入設定 Enctype=Multipart/Form


        使用ASP.NET 制作一個可以存放Client 端檔案的網頁相當容易,因為ASP.NET 里就有提供我們將Client 端檔案傳至Server 端的對象,這個對象就是HtmlInputFile 對象。HtmlInputFile 對象必須存在窗體中,而且窗體<Form> 標注中必須加入設定Enctype="Multipart/Form-Data" 屬性才可使用。HtmlInputFile 對象的語法如下所示:

    <Input Type="File" Id="被程序所控制的名稱" Runat="Server">

        當一個檔案傳送到Server 端后,接收和處理的是HtmlInputFile 對象的PostedFile 屬性。PostedFile 屬性的型態是HttpPostedFile 對象類別,其常用屬性如下表所示:


    其常用方法如下表所示:



    基本檔案上傳
    下列范例使用HtmlInputFile 對象的基本用法,分別印出檔案的名稱、大小、類型:

    <Html>
    <Form Enctype="Multipart/Form-Data" Runat="Server">
    <Input Type="File" Id="UpLoad" Runat="Server">
    <Asp:Button Id="Button1" Text="OK" OnClick="Button1_Click"
    Runat="Server" /><br>
    <Asp:Label Id="Label1" Runat="Server" />
    </Form>
    <Script Language="VB" Runat="Server">
    Sub Button1_Click(Sender As Object,e As Eventargs)
    Dim strContent As String
    If UpLoad.PostedFile.ContentLength<0 Then
    Label1.Text="上傳失敗"
    Else
    strContent="文件名稱:" & UpLoad.PostedFile.FileName
    strContent=strContent & "<br>檔案大?。?quot; & _
    Cstr(UpLoad.PostedFile.ContentLength) & "Bytes"
    strContent=strContent & "<br>檔案類型:" &
    UpLoad.PostedFile.ContentType
    End If
    Label1.Text=strContent
    End Sub
    </Script>
    </Html>



    存入磁盤
        上傳檔案后若要寫入磁盤中也相當容易,只要使用PostedFile 的SaveAs 方法即可。檔案寫入時若直接將PostedFile.FileName 屬性來當文件名寫入會有問題,因為FileName 屬性包含有檔案的路徑名稱,所以必須先將檔案的路徑分開。下列范例使用Split 函數來取得文件名稱,并將檔案存放到網頁所在的目錄:

    <Html>
    <Form Enctype="Multipart/Form-Data" Runat="Server">
    <Input Type="File" Id="UpLoad" Runat="Server">
    <Asp:Button Id="Button1" Text="OK" OnClick="Button1_Click"
    Runat="Server" /><br>
    <Asp:Label Id="Label1" Runat="Server" />
    </Form>
    <Script Language="VB" Runat="Server">
    Sub Button1_Click(Sender As Object,e As Eventargs)
    Dim strContent As String
    If UpLoad.PostedFile.ContentLength>0 Then
    strContent="文件名稱:" & UpLoad.PostedFile.FileName
    strContent=strContent & "<br>檔案大?。?quot; &
    Cstr(UpLoad.PostedFile.ContentLength) & "Bytes"
    strContent=strContent & "<br>檔案類型:" &
    UpLoad.PostedFile.ContentType
    Dim Temp() As String = Split(UpLoad.PostedFile.FileName, "\" )
    Dim Name As String = Temp(Temp.Length-1)
    UpLoad.PostedFile.SaveAs(Server.MapPath(".") & "\" & Name)
    Label1.Text=strContent
    Else
    Label1.Text="上傳失敗"
    End If
    End Sub
    </Script>
    </Html>

        Split 函數會將一個字符串以我們設定的條件為分隔,并將分段后的字符串存至一個字符串數組中;所以我們只要以"\" 為條件來分隔檔案,在字符串數組中的最后一個元素即為文件名稱。

    多檔上傳
        我們也可以一次上傳多個檔案。管理檔案的對象是Request 對象的Files 屬性,它的型別是HttpFileCollection 集合對象類別,它的項目成員是HttpPostedFile 類別對象。HttpFileCollection型別變量的常用屬性如下表所示:



    其常用方法如下表所示:



    下列范例中我們放了兩個HtmlInputFile 對象,并上傳兩個檔案:

    <Html>
    <Form Enctype="Multipart/Form-Data" Runat="Server">
    <Input Type="File" Id="UpLoad1" Runat="Server"><br>
    <Input Type="File" Id="UpLoad2" Runat="Server">
    <Asp:Button Id="Button1" Text="OK" OnClick="Button1_Click"
    Runat="Server"/><br>
    <Asp:Label Id="Label1" Runat="Server"/>
    </Form>
    <Script Language="VB" Runat="Server">
    Sub Button1_Click(Sender As Object,e As EventArgs)
    Dim shtI As short
    Dim strContent As String
    Dim Files As HttpFileCollection
    Files=Request.Files
    For shtI=0 To Files.Count-1
    If Files.Item(shtI).ContentLength>0 Then
    strContent=strContent & "第" & Cstr(shtI+1) & "個檔案上傳對象
    <br>"
    strContent=strContent & "文件名稱:" & Files.Item(shtI).FileName
    & "<br>"
    strContent=strContent & "檔案大?。?quot; &
    Cstr(Files.Item(shtI).ContentLength)_
    & "Byte<br>"
    strContent=strContent & "檔案類型:" &
    Files.Item(shtI).ContentType & "<p>"
    Dim Temp() As String = Split( Files.Item(shtI).FileName, "\")
    Dim Name As String = Temp(Temp.Length-1)
    Files.Item(shtI).SaveAs(Server.MapPath(".") & "\" & Name)
    End If
    Next
    Label1.Text=strContent
    End Sub
    </Script>
    </Html>


        上述范例中我們先定義一個HttpFileCollection 類型變量Files,并將Request.Files 屬性的內容放到Files 變量中。再使用For....Next 循環將檔案信息加入strContent 變量中,再以Split 函數將檔案路徑和文件名稱分開并且存到一數組,因數組的最后一個元素即為文件名稱,因此個別取出后使用HttpPostedFile 對象的SaveAs 方法將檔案寫入磁盤。

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>