Uppic.aspx.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. public partial class EditMenu_Uppic : System.Web.UI.Page
  9. {
  10. LYS.UserData1 myud = new LYS.UserData1();
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. }
  14. protected void btnUpfiles_Click(object sender, EventArgs e)
  15. {
  16. string fileDir = Request.QueryString["Dir"].ToString();
  17. string tableName = Request.QueryString["tn"].ToString();
  18. string SaveFieldName = Request.QueryString["FieldN"].ToString();
  19. int KeyF = Convert.ToInt32(Request.QueryString["KeyF"].ToString());
  20. //HttpPostedFile MyPostedfile = this.FileUpload1.PostedFile;
  21. Literal1.Text =UpfilesPathandToDBA(FileUpload1, fileDir, true, tableName, SaveFieldName, KeyF).ToString();
  22. //Literal1.Text = myud.Upfiles(MyPostedfile,fileDir,true,tableName,SaveFieldName,KeyF).ToString();
  23. }
  24. /// <summary> 上传文件(所有类型,没有限制,以原文件名或者日期为文件名保存) </summary>
  25. /// <param name="fileupload">WebControls.FileUpload 控件</param>
  26. /// <param name="Fdir">保存的文件夹</param>
  27. /// <param name="OldFilenameOrNewFileName">是以原文件名保存还是以日期为文件名保存</param>
  28. //调用:
  29. //Literal1.Text = myud.UpfilesPathandToDB(FileUpload1, "upfile", true, "TreeGJK","URL",23).ToString();
  30. public string UpfilesPathandToDBA(System.Web.UI.WebControls.FileUpload fileupload, string Fdir, Boolean OldFilenameOrNewFileName, string TableName, string SaveFieldName, Int32 KeyID)
  31. {// OldFilenameOrNewFileName 为True 时,以日期为文件名,否则以原文件名
  32. HttpPostedFile postedfile = fileupload.PostedFile;
  33. //if (!Directory.Exists(Server.MapPath(".") + "PIC"))
  34. //{ Directory.CreateDirectory(Server.MapPath(".") + "PIC"); }
  35. StringBuilder strmsg = new StringBuilder();
  36. string UpFilename = System.IO.Path.GetFileName(postedfile.FileName);
  37. string UpfileExtension = System.IO.Path.GetExtension(postedfile.FileName);
  38. string Newfilename = DateTime.Now.ToString("yyyyMMddHHmmssffff") + UpfileExtension;
  39. if (OldFilenameOrNewFileName == true) { UpFilename = Newfilename; }
  40. if (postedfile.ContentLength > 0)
  41. {
  42. if (TableName.Length > 0 && KeyID > 0)
  43. { myud.UpdateStringFieldValue(TableName, SaveFieldName, UpFilename.ToString(), KeyID); }
  44. if (Fdir == "")
  45. { postedfile.SaveAs(HttpContext.Current.Server.MapPath(".") + "\\" + UpFilename); }
  46. else
  47. {
  48. // Response.Write(HttpContext.Current.Server.MapPath("Upfiles"));
  49. // Response.End();
  50. postedfile.SaveAs(HttpContext.Current.Server.MapPath("Upfiles") + "\\" + Fdir + "\\" + UpFilename);
  51. }
  52. strmsg.AppendFormat("【文件名:" + UpFilename.ToString() + "】");
  53. strmsg.AppendFormat("<br>【文件大小:" + postedfile.ContentLength.ToString() + "】");
  54. strmsg.AppendFormat("<br>【文件类型:" + postedfile.ContentType.ToString() + "】");
  55. return strmsg.ToString();
  56. }
  57. else
  58. { return "请选择欲上传文件"; }
  59. }
  60. }