| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace XYFDRQ.Common
- {
- public class UpLoadFiles
- {
- /// <summary>
- /// 文件上传基类
- /// </summary>
- public class upfile
- {
- private string path = null;
- private string fileType = null;
- private int sizes = 0;
- /// <summary>
- /// 初始化变量
- /// </summary>
- public upfile()
- {
- path = ""; //上传路径
- fileType = ""; //上传文件类型限制
- sizes = 1024 * 1024; //传文件的大小,默认1024KB
- }
- /// <summary>
- /// 初始化变量
- /// </summary>
- /// <param name="path">文件上传路径</param>
- /// <param name="fileType">文件上传类型</param>
- /// <param name="sizes">文件限制大小</param>
- public upfile(string path, string fileType, int sizes)
- {
- this.Path = path;
- this.FileType = fileType;
- this.Sizes = sizes;
- }
- /// <summary>
- /// 设置上传路径,如:uploadimages/
- /// </summary>
- public string Path
- {
- set
- {
- path = "/" + value + "/";
- }
- }
- /// <summary>
- /// 设置上传文件大小,单位为KB
- /// </summary>
- public int Sizes
- {
- set
- {
- sizes = value * 1024;
- }
- }
- /// <summary>
- /// 设置上传文件的类型,如:jpg|gif|bmp
- /// </summary>
- public string FileType
- {
- set
- {
- fileType = value;
- }
- }
- /// <summary>
- /// 文件上传基类
- /// </summary>
- /// <param name="name">控件名称</param>
- /// <param name="dept">部门名称</param>
- /// <returns>文件路径</returns>
- public string fileSaveAs(System.Web.UI.HtmlControls.HtmlInputFile name, string dept)
- {
- string filePath = null; long strLen = 0;
- try
- {
- //待文件上传路径
- string upLoadPath = null;
- //所属以当前月份的文件夹名称
- string monthFiles = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
- string mappathinfo = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + path;
- if (!Directory.Exists(mappathinfo + dept + "\\" + monthFiles.Trim()))
- {
- Directory.CreateDirectory(mappathinfo + dept + "\\" + monthFiles.Trim());
- }
- upLoadPath = mappathinfo + dept + "\\" + monthFiles.Trim();
- //获得文件的上传的路径
- string sourcePath = name.Value.Trim();
- //判断上传文件是否为空
- if (sourcePath == "" || sourcePath == null)
- {
- message("您没有上传数据!");
- return null;
- }
- 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)
- {
- message("该文件已存在,请查验或修改文件名称");
- return null;
- }
- // 获得文件扩展名
- string tFileType = sourcePath.Substring(sourcePath.LastIndexOf(".") + 1);
- //获得上传文件的大小
- strLen = name.PostedFile.ContentLength;
- //分解允许上传文件的格式
- string[] temp = fileType.Split('|');
- //设置上传的文件是否是允许的格式
- bool flag = false;
- //判断上传文件大小
- if (strLen >= sizes)
- {
- message("上传的数据不能大于" + sizes + "KB");
- return null;
- }
- //判断上传的文件是否是允许的格式
- foreach (string data in temp)
- {
- if (data == tFileType)
- {
- flag = true;
- break;
- }
- }
- //如果为真允许上传,为假则不允许上传
- if (!flag)
- {
- message("目前本系统支持的格式为:" + fileType);
- return null;
- }
- name.PostedFile.SaveAs(filePath);
- filePath = monthFiles + "/" + filename;
- //message("上传成功");
- }
- catch
- {
- //异常
- message("出现未知错误!");
- return null;
- }
- return filePath + "," + strLen;
- }
- private void message(string msg, string url)
- {
- System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('" + msg + "');window.location='" + url + "'</script>");
- }
- private void message(string msg)
- {
- System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('" + msg + "');</script>");
- }
- }
- }
- }
|