多文件上传
第一种方法
//上传文件 public void FN_UpFiles() { //遍历File表单元素 HttpFileCollection files = HttpContext.Current.Request.Files; //StringBuilder strMsg = new StringBuilder(); //strMsg.Append("上传的文件分别是:<hr color='pink'/>"); try { for (int iFile = 0 ; iFile < files.Count ; iFile++) { //检查文件扩展名字 HttpPostedFile postedFile = files[iFile]; string fileName = ""; //string fileExtension = ""; fileName = Path.GetFileName(postedFile.FileName); if (fileName != "") { try { string strpath = HttpContext.Current.Request.MapPath("~/ResourcesFolder/") + fileName; if (System.IO.File.Exists(strpath)) { Response.Write("已经存在文件:" + fileName + "<br>"); } else { try { NRModel.File model = new NRModel.File(); NRBLL.File bf = new NRBLL.File(); Guid guid1 = Guid.NewGuid(); Guid guid2 = Guid.NewGuid(); Guid guid3 = Guid.NewGuid(); Guid guid4 = Guid.NewGuid(); model.Fileid = guid1; model.Folderid = guid2; model.Filepath = strpath; model.FileNam = fileName.ToString(); model.FileSize = postedFile.ContentLength; model.Decription = this.decrition.Value; model.CreateOn = DateTime.Now; model.CreateBy = guid3; model.ModefyBy = guid4; if (bf.FN_AddNewRes(model) > 0) { //fileExtension = Path.GetExtension(fileName); //strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>"); //strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>"); //strMsg.Append("上传文件的文件名:" + fileName + "<br>"); //strMsg.Append("上传文件的扩展名:" + fileExtension + "<br>"); //strMsg.Append("上传文件的大小:" + postedFile.ContentLength.ToString() + "个字节" + "<br>"); postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("~/ResourcesFolder/") + fileName); Page.RegisterStartupScript("提示", "<script language='javascript'>alert('上传成功!')</script>"); Response.Write("<script language='javascript'>self.opener.location.reload();</script>"); Response.Write("<script language='javascript'>window.close();</script>"); } } catch (Exception ex) { Response.Write(ex.ToString()); } } } catch (Exception ex) { Response.Write(ex.ToString()); } } else { Page.RegisterStartupScript("提示", "<script language='javascript'>alert('没有添加上传文件!')</script>"); } } //strStatus.Text = strMsg.ToString(); } catch (System.Exception ex) { Response.Write(ex.ToString()); }第二种方法
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void bt_upload_Click(object sender, EventArgs e) { if (FileUpload1.PostedFile.FileName == "" && FileUpload2.PostedFile.FileName == "" && FileUpload3.PostedFile.FileName == "") { this.lb_info.Text = "请选择文件!"; } else { HttpFileCollection myfiles = Request.Files; for (int i = 0; i < myfiles.Count; i++) { HttpPostedFile mypost = myfiles[i]; try { if (mypost.ContentLength > 0) { string filepath = mypost.FileName;//C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);//20022775_m.jpg string serverpath = Server.MapPath("~/images/") + filename;//C:\Inetpub\wwwroot\WebSite2\images\20022775_m.jpg mypost.SaveAs(serverpath); this.lb_info.Text = "上传成功!"; } } catch (Exception ex) { this.lb_info.Text = "上传发生错误!原因:" + ex.Message.ToString(); } } } } }

浙公网安备 33010602011771号