Example #1
0
        public void ApplyEffects(DragEventArgs e)
        {
            string[] fileNameArray = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            if (!e.Data.GetDataPresent(System.Windows.Forms.DataFormats.FileDrop))
            {
                /* ファイル以外のドラッグは受け入れない */
                e.Effect = DragDropEffects.None;
                return;
            }

            if (!MultiFile && fileNameArray.Length > 1)
            {
                /* 複数ファイルのドラッグは受け入れない */
                e.Effect = DragDropEffects.None;
                return;
            }
            foreach (var item in fileNameArray)
            {
                if (!AllowExt.Select(x => x.ToLower()).Contains(Path.GetExtension(item).ToLower()))
                {
                    /* 拡張子が.xls以外は受け入れない */
                    e.Effect = DragDropEffects.None;
                    return;
                }
            }

            /* 上記以外は受け入れる */
            e.Effect = DragDropEffects.All;
        }
Example #2
0
        /// <summary>
        /// 检查上传文件是否合法
        /// </summary>
        /// <param name="fileBuffer">文件流</param>
        /// <param name="fileExt">文件后缀</param>
        /// <returns>是否合法</returns>
        private CheckResult CheckedUploadFile(byte[] fileBuffer, string fileExt)
        {
            if (fileBuffer.Length == 0)
            {
                return(CheckResult.Fail("无数据提交"));
            }

            if (fileBuffer.Length > this.MaxFilesize)
            {
                return(CheckResult.Fail("文件大小超过" + this.MaxFilesize + "字节"));
            }

            if (!AllowExt.Contains(fileExt))
            {
                return(CheckResult.Fail("上传文件扩展名必需为:" + string.Join(",", AllowExt)));
            }

            return(CheckResult.Success());
        }
Example #3
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Charset = "UTF-8";

            byte[] file;
            string localFileName = string.Empty;
            string err           = 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上传
                file          = 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长度.
                file = new Byte[postedfile.ContentLength];

                // 转换为byte类型
                System.IO.Stream stream = postedfile.InputStream;
                stream.Read(file, 0, postedfile.ContentLength);
                stream.Close();

                filecollection = null;
            }

            var ext = localFileName.Substring(localFileName.LastIndexOf('.') + 1).ToLower();

            if (file.Length == 0)
            {
                err = "无数据提交";
            }
            else if (file.Length > this.MaxFilesize)
            {
                err = "文件大小超过" + this.MaxFilesize + "字节";
            }
            else if (!AllowExt.Contains(ext))
            {
                err = "上传文件扩展名必需为:" + string.Join(",", AllowExt);
            }
            else
            {
                var folder             = context.Request["subfolder"] ?? "default";
                var uploadFolderConfig = UploadConfigContext.UploadConfig.UploadFolders.FirstOrDefault(u => string.Equals(folder, u.Path, StringComparison.OrdinalIgnoreCase));
                var 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_" + ext;
                    break;

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

                fileFolder = Path.Combine(UploadConfigContext.UploadPath,
                                          folder,
                                          subFolder
                                          );

                filePath = Path.Combine(fileFolder,
                                        string.Format("{0}{1}.{2}", DateTime.Now.ToString("yyyyMMddhhmmss"), new Random(DateTime.Now.Millisecond).Next(10000), ext)
                                        );

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

                var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
                fs.Write(file, 0, file.Length);
                fs.Flush();
                fs.Close();

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

                this.OnUploaded(context, filePath);
            }

            file = null;
            context.Response.Write(this.GetResult(localFileName, filePath, err));
            context.Response.End();
        }
Example #4
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();
        }
Example #5
0
        public void ProcessRequest(HttpContext context)
        {
            byte[] buffer;
            context.Response.Charset = "UTF-8";
            string localFileName;
            var    err   = string.Empty;
            var    str3  = string.Empty;
            var    path  = string.Empty;
            var    input = context.Request.ServerVariables["HTTP_CONTENT_DISPOSITION"];

            if (input != null)
            {
                buffer        = context.Request.BinaryRead(context.Request.TotalBytes);
                localFileName = Regex.Match(input, "filename=\"(.+?)\"").Groups[1].Value;
            }
            else
            {
                var file = context.Request.Files.Get(FileInputName);
                localFileName = Path.GetFileName(file.FileName);
                buffer        = new byte[file.ContentLength];
                var inputStream = file.InputStream;
                inputStream.Read(buffer, 0, file.ContentLength);
                inputStream.Close();
            }
            var str7 = localFileName.Substring(localFileName.LastIndexOf('.') + 1).ToLower();

            if (buffer.Length == 0)
            {
                err = "无数据提交";
            }
            else if (buffer.Length > MaxFilesize)
            {
                err = "文件大小超过" + MaxFilesize + "字节";
            }
            else if (!AllowExt.Contains(str7))
            {
                err = "上传文件扩展名必需为:" + string.Join(",", AllowExt);
            }
            else
            {
                var folder  = context.Request["subfolder"] ?? "default";
                var Ufolder = UploadConfigContext.UploadConfig.UploadFolders.FirstOrDefault(u => string.Equals(folder, u.Path, StringComparison.OrdinalIgnoreCase));
                switch ((Ufolder.DirType))
                {
                case DirType.Day:
                    str3 = "day_" + DateTime.Now.ToString("yyMMdd");
                    break;

                case DirType.Month:
                    str3 = "month_" + DateTime.Now.ToString("yyMM");
                    break;

                case DirType.Ext:
                    str3 = "ext_" + str7;
                    break;
                }
                var str4 = Path.Combine(UploadConfigContext.UploadPath, folder, str3);
                path = Path.Combine(str4, string.Format("{0}{1}.{2}", DateTime.Now.ToString("yyyyMMddhhmmss"), new Random(DateTime.Now.Millisecond).Next(0x2710), str7));
                if (!Directory.Exists(str4))
                {
                    Directory.CreateDirectory(str4);
                }
                var stream2 = new FileStream(path, FileMode.Create, FileAccess.Write);
                stream2.Write(buffer, 0, buffer.Length);
                stream2.Flush();
                stream2.Close();
                if (ImageExt.Contains(str7))
                {
                    ThumbnailService.HandleImmediateThumbnail(path);
                }
                OnUploaded(context, path);
            }
            context.Response.Write(GetResult(localFileName, path, err));
            context.Response.End();
        }