Example #1
0
        public void Streaming_Whole_File_Should_Create_Exact_Copy()
        {
            Random rnd = new Random();

            byte[] buffer = new byte[12];
            rnd.NextBytes(buffer);

            MemoryStream ms           = new MemoryStream(buffer);
            var          uploadedFile = UploadFolder.AddFile("rnd.bin", ms, false, 12, "");

            byte[] copy = new byte[12];
            using (var stream = uploadedFile.GetContents())
            {
                stream.Read(copy, 0, 12);
            }

            for (int i = 0; i < 12; i++)
            {
                Assert.AreEqual(buffer[i], copy[i]);
            }


            Console.Out.WriteLine(Environment.CurrentDirectory);
            Console.Out.WriteLine("done");
        }
Example #2
0
        /// <summary>
        /// 根据配置规则生成上传文件存储路径
        /// </summary>
        /// <param name="context">HttpContext</param>
        /// <param name="fileExt">文件后缀</param>
        /// <returns>文件路径</returns>
        private string BuilderUploadFilePath(HttpContext context, string fileExt)
        {
            string            _filePath           = string.Empty;
            string            _folder             = context.Request["subfolder"] ?? "default";
            UploadFolder      _uploadFolderConfig = UploadConfigContext.UploadConfig.UploadFolders.FirstOrDefault(u => string.Equals(_folder, u.Path, StringComparison.OrdinalIgnoreCase));
            UploadSaveDirType _dirType            = _uploadFolderConfig == null ? UploadSaveDirType.Day : _uploadFolderConfig.DirType;
            string            _subFolder          = string.Empty;
            string            _fileFolder         = string.Empty;

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

            case UploadSaveDirType.Ext:
                _subFolder = "ext_" + fileExt;
                break;

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

            _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);
            }

            return(_filePath);
        }
Example #3
0
        public static string FileName(HttpPostedFileBase file, UploadFolder f)
        {
            string dosyaAdi = "no_image";
            string uzanti   = ".png";

            if (file != null)
            {
                dosyaAdi = System.IO.Path.GetFileNameWithoutExtension(file.FileName);
                uzanti   = System.IO.Path.GetExtension(file.FileName);

                int    count = 0;
                string path  = System.IO.Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/Uploads"), f.ToString(), $"{dosyaAdi}{uzanti}");

                while (System.IO.File.Exists(path))
                {
                    count++;
                    dosyaAdi = $"{System.IO.Path.GetFileNameWithoutExtension(file.FileName)}({count.ToString()})";
                    path     = System.IO.Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/Uploads"), f.ToString(), $"{dosyaAdi}{uzanti}");
                }

                file.SaveAs(path);
            }
            return($"{dosyaAdi}{uzanti}");
        }
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 Task <Guid> UploadFolder(UploadFolder command) => Send(command);