附件,图片等上传代码
<!--Html--> <td colspan="2"><input type='file' name='txfile' require='true' datatype='require' style="width:468px;" class="write_3" />
HttpPostedFile tx = Request.Files["txfile"]; if (tx.FileName != null && tx.FileName.ToString() != "") { if (tx.ContentLength > 1024 * 50) { base.Alert("上传图标不能超过50KB"); return; } oldtx = UpLoad(tx); } //method public string UpLoad(HttpPostedFile hpf) { string strExt = hpf.FileName.Remove(0, hpf.FileName.LastIndexOf(".")); string[] Er = { ".jpg", ".png", ".bmp" }; string lower = strExt.ToLower(); int len = Er.Length; bool btrue = false; for (int num = 0; num < len; num++) { if (lower == Er[num]) { btrue = true; } } if (!btrue) { //"图标格式不正确,请上传图标"; return ""; } Random ra = new Random(); DateTime datNow = DateTime.Now; string MyFileName = datNow.Year.ToString() + datNow.Month.ToString() + datNow.Day.ToString() + datNow.Hour.ToString() + datNow.Minute.ToString() + datNow.Second.ToString() + datNow.Millisecond.ToString(); string strNewName = MyFileName + ra.Next(10000) + hpf.FileName.Remove(0, hpf.FileName.LastIndexOf("."));//文件名 string strpath = Server.MapPath("~/UploadFiles/affix/") + strNewName; hpf.SaveAs(strpath); return "/UploadFiles/affix/" + strNewName;//相对路径 }