| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- using System;
- using System.IO;
- using System.Web;
- using System.Web.UI.HtmlControls;
- namespace Common
- {
- public class UpLoadFiles
- {
- public class upfile
- {
- private string path = null;
- private string fileType = null;
- private int sizes = 0;
- public string Path
- {
- set
- {
- this.path = "/" + value + "/";
- }
- }
- public int Sizes
- {
- set
- {
- this.sizes = value * 1024;
- }
- }
- public string FileType
- {
- set
- {
- this.fileType = value;
- }
- }
- public upfile()
- {
- this.path = "";
- this.fileType = "";
- this.sizes = 409600000;
- }
- public string fileSaveAs(HtmlInputFile name, string dept)
- {
- string filePath = null;
- string result;
- try
- {
- string monthFiles = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
- string mappathinfo = HttpContext.Current.Request.PhysicalApplicationPath + this.path;
- if (!Directory.Exists(mappathinfo + dept + "\\" + monthFiles.Trim()))
- {
- Directory.CreateDirectory(mappathinfo + dept + "\\" + monthFiles.Trim());
- }
- string upLoadPath = mappathinfo + dept + "\\" + monthFiles.Trim();
- string sourcePath = name.Value.Trim();
- if (sourcePath == "" || sourcePath == null)
- {
- this.message("您没有上传数据!");
- result = null;
- return result;
- }
- int start = sourcePath.LastIndexOf("\\") + 1;
- int end = sourcePath.Length;
- string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + sourcePath.Substring(start, end - start);
- filePath = upLoadPath + "\\" + filename;
- FileInfo _file = new FileInfo(filePath);
- if (_file.Exists)
- {
- this.message("该文件已存在,请查验或修改文件名称");
- result = null;
- return result;
- }
- string tFileType = sourcePath.Substring(sourcePath.LastIndexOf(".") + 1);
- long strLen = (long)name.PostedFile.ContentLength;
- string[] temp = this.fileType.Split(new char[]
- {
- '|'
- });
- bool flag = false;
- if (strLen >= (long)this.sizes)
- {
- this.message("上传的数据不能大于" + this.sizes + "KB");
- result = null;
- return result;
- }
- string[] array = temp;
- for (int i = 0; i < array.Length; i++)
- {
- string data = array[i];
- if (data == tFileType)
- {
- flag = true;
- break;
- }
- }
- if (!flag)
- {
- this.message("目前本系统支持的格式为:" + this.fileType);
- result = null;
- return result;
- }
- name.PostedFile.SaveAs(filePath);
- filePath = monthFiles + "/" + filename;
- }
- catch
- {
- this.message("出现未知错误!");
- result = null;
- return result;
- }
- result = filePath;
- return result;
- }
- private void message(string msg, string url)
- {
- HttpContext.Current.Response.Write(string.Concat(new string[]
- {
- "<script language=javascript>alert('",
- msg,
- "');window.location='",
- url,
- "'</script>"
- }));
- }
- private void message(string msg)
- {
- HttpContext.Current.Response.Write("<script language=javascript>alert('" + msg + "');</script>");
- }
- }
- }
- }
|