Esempio n. 1
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        private static UploadResult UploadFile(UploadParameter p)
        {
            var file = p.File;

            if (file == null || file.ContentLength < 1)
            {
                return(new UploadResult(UploadStatus.NoFile, "没有选择上传文件"));
            }
            // 检查文件大小
            int    size   = file.ContentLength;
            double maxLen = p.MaxLength * 1024d * 1024d;

            if (size > maxLen)
            {
                string msg = string.Format("文件大小超出限制,最大允许上传[{0}]MB", p.MaxLength);
                return(new UploadResult(UploadStatus.OutOfSize, msg));
            }
            //取得上传文件之扩展文件名,并转换成小写字母
            string fileExtension = Path.GetExtension(file.FileName);
            bool   fileAllow     = p.AllowExtension(fileExtension);

            if (!fileAllow)
            {
                string msg = string.Format("文件类型受限制,只允许上传[{0}]格式", string.Join(",", p.Extensions));
                return(new UploadResult(UploadStatus.OutOfFormat, msg));
            }
            try
            {
                string        savePath = p.SavePath;
                DirectoryInfo di       = new DirectoryInfo(Path.GetDirectoryName(savePath));
                if (!di.Exists)
                {
                    di.Create();
                }
                file.SaveAs(savePath);
                return(new UploadResult(UploadStatus.Success, "上传成功"));
            }
            catch (Exception e)
            {
                return(new UploadResult(UploadStatus.Error, "上传出现错误--" + e.Message));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="p">其他参数</param>
        /// <returns></returns>
        public static UploadResult UploadFile(UploadType type, UploadParameter p)
        {
            switch (type)
            {
            case UploadType.IMAGE:
                p.Extensions = new string[] { ".jpg", ".jpeg", ".png", ".bmp", ".gif" };
                p.MaxLength  = 20;
                break;

            case UploadType.RAR:
                p.Extensions = new string[] { ".rar", ".zip" };
                break;

            case UploadType.EXCEL:
                p.Extensions = new string[] { ".xls", ".xlsx" };
                break;

            case UploadType.FILE:
                break;
            }

            return(UploadFile(p));
        }