-
-
-
-
-
-
- public static string UploadFile(HttpPostedFile pf,string savepath, out string filename)
- {
- string fileExt = GetFileExt(pf.FileName);
- if (!AllowFileExt(fileExt))
- {
- filename = string.Empty;
- return "上传文件扩展名不被允许";
- }
- else if (pf.ContentLength >1048576)
- {
- filename = string.Empty;
- return "上传文件大小不得超过1MB";
- }
-
- filename = GetNewFileName() + "." + fileExt;
- string path = HttpContext.Current.Server.MapPath("~" + "/" + savepath + "/" + filename);
- pf.SaveAs(path);
- return string.Empty;
- }
-
-
-
-
-
- public static string GetNewFileName()
- {
- return DateTime.Now.ToString("MMddhhmmss");
- }
-
-
-
-
-
-
- static public string GetFileExt(string filename)
- {
- int pos = filename.LastIndexOf(".");
- return filename.Substring(pos + 1);
- }
-
-
-
-
-
-
- static public string GetFileName(string filename)
- {
- int pos = filename.LastIndexOfAny(new char[] { '\\', '/' });
- int pos1 = 0;
- int len = pos - pos1;
- return filename.Substring(pos1 + 1, len);
- }
-
-
-
-
-
- static public bool AllowFileExt(string ext)
- {
- try
- {
- string ex = "txt,xls,doc,docx,rar,zip";
- foreach (string s in ex.Split(','))
- {
- if (ext.ToLower() == s.ToLower())
- {
- return true;
- }
- }
- }
- catch { return true; }
- return false;
- }
-
- <asp:FileUpload ID="FileUpload1" runat="server" CssClass="fileupload" />
-
-
-
- protected void Button1_Click(object sender, EventArgs e)
- {
- string filename = "";
- string msg = Common .UploadFile(FileUpload1.PostedFile, "upload", out filename);
- if ("" == msg)
- {
- attendInfo.AttachUrl = filename;
- return true;
- }
- else
- {
- Common.ShowMessage(""+msg+"");
- return false;
- }
posted @
2011-09-28 11:03
星月磊子
阅读(
167)
评论()
编辑
收藏
举报