Beispiel #1
0
 /// <summary>
 /// 处理上传文件是图片类型的时候
 /// </summary>
 /// <param name="filePath">路径</param>
 /// <param name="fileExt">文件后缀</param>
 private void HanlderUploadImageFile(string filePath, string fileExt)
 {
     //是图片,即使生成对应尺寸
     if (ImageExt.Contains(fileExt))
     {
         ThumbnailService.HandleImmediateThumbnail(filePath);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="context">The context.</param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Charset = "UTF-8";
            byte[] _fileBuffer;
            string _localFileName = string.Empty;
            string _errMessage    = string.Empty;
            string _subFolder     = string.Empty;
            string _fileFolder    = string.Empty;
            string _filePath      = string.Empty;;
            var    disposition    = context.Request.ServerVariables["HTTP_CONTENT_DISPOSITION"];

            if (disposition != null)
            {
                // HTML5上传
                _fileBuffer    = context.Request.BinaryRead(context.Request.TotalBytes);
                _localFileName = Regex.Match(disposition, "filename=\"(.+?)\"").Groups[1].Value;// 读取原始文件名
            }
            else
            {
                HttpFileCollection _filecollection = context.Request.Files;
                HttpPostedFile     _postedfile     = _filecollection.Get(this.FileInputName);
                // 读取原始文件名
                _localFileName = Path.GetFileName(_postedfile.FileName);
                // 初始化byte长度.
                _fileBuffer = new byte[_postedfile.ContentLength];
                // 转换为byte类型
                using (Stream stream = _postedfile.InputStream)
                {
                    stream.Read(_fileBuffer, 0, _postedfile.ContentLength);
                    _filecollection = null;
                }
            }

            string _fileExt = _localFileName.Substring(_localFileName.LastIndexOf('.') + 1).ToLower();

            if (_fileBuffer.Length == 0)
            {
                _errMessage = "无数据提交";
            }
            else if (_fileBuffer.Length > this.MaxFilesize)
            {
                _errMessage = "文件大小超过" + this.MaxFilesize + "字节";
            }
            else if (!AllowExt.Contains(_fileExt))
            {
                _errMessage = "上传文件扩展名必需为:" + string.Join(",", AllowExt);
            }
            else
            {
                string       _folder             = context.Request["subfolder"] ?? "default";
                UploadFolder _uploadFolderConfig = UploadConfigContext.UploadConfig.UploadFolders.FirstOrDefault(u => string.Equals(_folder, u.Path, StringComparison.OrdinalIgnoreCase));
                DirType      _dirType            = _uploadFolderConfig == null ? DirType.Day : _uploadFolderConfig.DirType;

                //根据配置里的DirType决定子文件夹的层次(月,天,扩展名)
                switch (_dirType)
                {
                case DirType.Month:
                    _subFolder = "month_" + DateTime.Now.ToString("yyMM");
                    break;

                case DirType.Ext:
                    _subFolder = "ext_" + _fileExt;
                    break;

                case DirType.Day:
                    _subFolder = "day_" + DateTime.Now.ToString("yyMMdd");
                    break;
                }

                //fileFolder = Path.Combine(UploadConfigContext.UploadPath,
                //                          folder,
                //                          subFolder
                //                         );
                _fileFolder = CombinePaths(UploadConfigContext.UploadPath,
                                           _folder,
                                           _subFolder);
                _filePath = Path.Combine(_fileFolder,
                                         string.Format("{0}{1}.{2}", DateTime.Now.ToString("yyyyMMddhhmmss"), new Random(DateTime.Now.Millisecond).Next(10000), _fileExt)
                                         );

                if (!Directory.Exists(_fileFolder))
                {
                    Directory.CreateDirectory(_fileFolder);
                }

                using (FileStream fs = new FileStream(_filePath, FileMode.Create, FileAccess.Write))
                {
                    fs.Write(_fileBuffer, 0, _fileBuffer.Length);
                    fs.Flush();
                }

                //是图片,即使生成对应尺寸
                if (ImageExt.Contains(_fileExt))
                {
                    ThumbnailService.HandleImmediateThumbnail(_filePath);
                }

                this.OnUploaded(context, _filePath);
            }

            _fileBuffer = null;
            context.Response.Write(this.GetResult(_localFileName, _filePath, _errMessage));
            context.Response.End();
        }