Asp.net_檔案上傳控制項FileUpload用法初步

來源:互聯網
上載者:User

1.aspx

            <asp:FileUpload ID="fudTest" runat="server" />            <asp:Button ID="btnUpload" runat="server" Text="上傳" OnClick="btnUpload_Click" />            <br />            <asp:Label ID="lblMessage" runat="server"></asp:Label>

1.aspx.cs

//上傳檔案按鈕    protected void btnUpload_Click(object sender, EventArgs e)    {        //定義儲存路徑        string savePath = "UploadFiles";        //是否存在目錄        if (!System.IO.Directory.Exists(Server.MapPath(savePath)))        {            //不存在建立檔案夾             System.IO.Directory.CreateDirectory(Server.MapPath(savePath) );        }        //上傳檔案        if (fudTest.HasFile)        {            try            {                fudTest.SaveAs(Server.MapPath(savePath) + "\\" + fudTest.FileName);                lblMessage.Text = lblMessage.Text+"用戶端路徑:" + fudTest.PostedFile.FileName + "<br>" +                              "檔案名稱:" + System.IO.Path.GetFileName(fudTest.FileName) + "<br>" +                              "副檔名:" + System.IO.Path.GetExtension(fudTest.FileName) + "<br>" +                              "檔案大小:" + fudTest.PostedFile.ContentLength + " KB<br>" +                              "檔案MIME類型:" + fudTest.PostedFile.ContentType + "<br>" +                              "儲存路徑:" + Server.MapPath(savePath) + "\\" + fudTest.FileName+                              "<hr>";            }            catch (Exception ex)            {                lblMessage.Text = "發生錯誤:" + ex.Message.ToString();                            }        }        else        {            lblMessage.Text = "沒有選擇要上傳的檔案。";        }    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.