Пример #1
0
        /// <summary>
        /// 单个上传文件
        /// </summary>
        /// <param name="file"></param>
        /// <param name="belongApp">所属应用,如文章article</param>
        /// <param name="belongAppId">所属应用ID,如文章id</param>
        /// <returns></returns>
        public UploadFileResultOuputDto Add(IFormFile file, string belongApp, string belongAppId)
        {
            _belongApp   = belongApp;
            _belongAppId = belongAppId;
            if (file != null && file.Length > 0 && file.Length < 10485760)
            {
                using (var binaryReader = new BinaryReader(file.OpenReadStream()))
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var data     = binaryReader.ReadBytes((int)file.Length);
                    UploadFile(fileName, data);

                    UploadFile filedb = new UploadFile
                    {
                        FilePath    = _dbFilePath,
                        Thumbnail   = _dbThumbnail,
                        FileName    = fileName,
                        FileSize    = file.Length.ToInt(),
                        FileType    = Path.GetExtension(fileName),
                        Extension   = Path.GetExtension(fileName),
                        BelongApp   = _belongApp,
                        BelongAppId = _belongAppId
                    };
                    service.Insert(filedb);
                    return(filedb.MapTo <UploadFileResultOuputDto>());
                }
            }
            else
            {
                Log4NetHelper.Error("文件过大");
                throw new Exception("文件过大");
            }
        }
Пример #2
0
        /// <summary>
        /// 单个上传文件
        /// </summary>
        /// <param name="file"></param>
        /// <param name="belongApp"></param>
        /// <param name="belongAppId"></param>
        /// <returns></returns>
        private UploadFileResultOuputDto Add(IFormFile file, string belongApp, string belongAppId)
        {
            _belongApp   = belongApp;
            _belongAppId = belongAppId;
            if (file != null && file.Length > 0 && file.Length < 10485760)
            {
                using (var binaryReader = new BinaryReader(file.OpenReadStream()))
                {
                    var fileName = string.Empty;
                    fileName = _fileName;

                    var data = binaryReader.ReadBytes((int)file.Length);
                    UploadFile(fileName, data);
                    ;
                    UploadFile filedb = new UploadFile
                    {
                        Id          = GuidUtils.CreateNo(),
                        FilePath    = _dbFilePath,
                        Thumbnail   = _dbThumbnail,
                        FileName    = fileName,
                        FileSize    = file.Length.ToInt(),
                        FileType    = Path.GetExtension(fileName),
                        Extension   = Path.GetExtension(fileName),
                        BelongApp   = _belongApp,
                        BelongAppId = _belongAppId
                    };
                    new UploadFileApp().Insert(filedb);
                    UploadFileResultOuputDto uploadFileResultOuputDto = filedb.MapTo <UploadFileResultOuputDto>();
                    uploadFileResultOuputDto.PhysicsFilePath = (_hostingEnvironment.WebRootPath + "/" + _dbThumbnail).ToFilePath();;
                    return(uploadFileResultOuputDto);
                }
            }
            else
            {
                Log4NetHelper.Info("文件过大");
                throw new Exception("文件过大");
            }
        }