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 = "不能上傳空文件"; } } }