結合asp.net的一個文件上傳示例" type="hidden"/>

HTML 結合asp.net的一個文件上傳示例


HTML的代碼:(關鍵是要在form里設置enctype="multipart/form-data",這樣才能在提交表單時,將文件以二進制流的形式傳輸到服務器)

    <form id="form1" runat="server"  method="post" enctype="multipart/form-data">
    <div>
    <input type="file" />
     <asp:Button ID="btnUpload" runat="server"  Text="開始上傳" onclick="btnUpload_Click" />
    </div>
    </form>

aps.net后台代碼(C#):

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            //int intCount = RequestClass.GetFormInt("hdcount", -1);
            HttpFileCollection Files = HttpContext.Current.Request.Files;
            for (int i = 0; i < Files.Count; i++)
            {
                HttpPostedFile PostedFile = Files[i];
                if (PostedFile.ContentLength > 0)
                {
                    string FileName = PostedFile.FileName;
                    string strExPrentFile = FileName.Substring(FileName.LastIndexOf(".") + 1);

                    string sFilePath = "/uploadfile/hotel/" + StringClass.makeFileName24() + i + strExPrentFile;
                    PostedFile.SaveAs(Server.MapPath(sFilePath));
                }
                else
                {
                    //this.LabMessage.Text = "不能上傳空文件";
                }
            }
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM