Example #1
0
        public IActionResult AddSeedAndImg(IFormFile file, FileUpDownLoadGetDto dtos)
        {
            if (file == null || file.ContentDisposition.Length <= 0 || file.FileName == null)
            {
                return(BadRequest("空异常"));
            }
            FileUpDownLoadAndFileGetDto dto = new FileUpDownLoadAndFileGetDto
            {
                file      = file,
                FileClass = dtos.FileClass,
                FileUrl   = dtos.FileUrl,
                SeedID    = dtos.SeedID
            };

            string fileUrl = _fileService.PrintFileCreate(dto.file);

            FileStreamPandO.FilesPrint fp = new FilesPrint();
            fileUrl = fp.SaveSRC(fileUrl);
            if (fileUrl == null)
            {
                return(StatusCode(500, "存储错误"));
            }
            if (_productRepository.GetSeed(dto.SeedID) == null)
            {
                return(StatusCode(500, "不存在该编号产品"));
            }
            var model = new FileUpDownload()
            {
                FileClass = dto.FileClass,
                FileName  = file.FileName,
                FileUrl   = fileUrl,

                seed = _productRepository.GetSeed(dto.SeedID)
            };

            _productRepository.AddFile(model);
            if (!_productRepository.Save())
            {
                return(StatusCode(500, "上传失败,转储数据库异常"));
            }
            return(NoContent());
        }
        /// <summary>
        /// 返回存储路径
        /// </summary>
        /// <param name="url"></param>
        /// <param name="file"></param>
        /// <returns>数据库存储路径!!! </returns>
        public string PrintFileCreate(IFormFile file)
        {
            var        fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
            string     filePath = ServiceConfigs.FileUpDirectory;
            FilesPrint fp       = new FilesPrint();
            string     suffix   = fileName.Split('.')[1];

            if ((!pictureFormatArray.Contains(suffix)))
            {
                _logger.LogWarning("+文件类型错误+");
                return(null);
            }
            filePath += @"\" + System.DateTime.Now.Year.ToString() + @"\" + System.DateTime.Now.Month.ToString() + @"\" + System.DateTime.Now.Day.ToString();//文件夹
            UnixStamp ustamp = new UnixStamp();

            if (!(Directory.Exists(filePath)))
            {
                Directory.CreateDirectory(filePath);
            }
            fileName = @"\" + ustamp.DateTimeToStamp(System.DateTime.Now) + "." + suffix;
            string fileFullName = filePath + fileName;

            try
            {
                using (FileStream fs = System.IO.File.Create(fileFullName))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
            }
            catch (Exception e)
            {
                string ss = e.ToString();
                _logger.LogError($"+存储异常+{ss}");
                return(null);
            }
            FileStreamPandO.FilesPrint f = new FilesPrint();
            return(f.SaveSRC(fileFullName));
        }