public ActionResult UploadImages()
        {
            var finalFileNames = new List <string>();

            try
            {
                foreach (string fileName in Request.Files)
                {
                    var image       = Request.Files[fileName];
                    var newFileName = FileUploadManager.UploadAndSave(image);

                    finalFileNames.Add(newFileName);
                }

                return(new JsonResult
                {
                    Data = new { FileNames = finalFileNames }
                });
            }
            catch (Exception ex)
            {
                ex.Log();

                finalFileNames.ForEach(a => FileUploadManager.DeleteFile(a));

                return(new JsonResult
                {
                    Data = new { Error = ex.Message }
                });
            }
        }
Example #2
0
        /// <summary>
        /// 处理增值税业务中的证书
        /// </summary>
        /// <param name="vat"></param>
        private void CertificateFileHandel(ValueAddedTaxInfo vat)
        {
            string fileIdentity  = vat.CertificateFileName;
            string getConfigPath = AppSettingManager.GetSetting("Customer", "CertificateFilesPath");

            if (!string.IsNullOrEmpty(fileIdentity))
            {
                if (!fileIdentity.Contains(getConfigPath))
                {
                    string fileName = Path.GetFileName(Encoding.UTF8.GetString(Convert.FromBase64String(fileIdentity)));
                    vat.CertificateFileName = getConfigPath + "\\" + fileName;
                    if (!Path.IsPathRooted(getConfigPath))
                    {
                        //是相对路径:
                        getConfigPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, getConfigPath);
                    }
                    string getDestinationPath = Path.Combine(getConfigPath, fileName);
                    string getFolder          = Path.GetDirectoryName(getDestinationPath);
                    if (!Directory.Exists(getFolder))
                    {
                        Directory.CreateDirectory(getFolder);
                    }
                    //将上传的文件从临时文件夹剪切到目标文件夹:
                    FileUploadManager.MoveFile(fileIdentity, getDestinationPath);

                    FileUploadManager.DeleteFile(fileIdentity);
                }
            }
        }
Example #3
0
        public ActionResult Add(Guid communityId, [FromForm] ConsumptionApiModel consumptionApiModel)
        {
            try
            {
                // Guid id = Guid.Parse(attachViewModel.Id);
                FileUploadManager fileUploadManager = new FileUploadManager(consumptionApiModel.Attachment);
                string            fileName          = fileUploadManager.FileName;

                var outputFilePath = FileUploadManager.GetTempFolderPath(Guid.NewGuid().ToString() + ".xlsx");
                var byteArray      = fileUploadManager.GetByteArray();
                var consumption    = _consumptionMapper.Map(consumptionApiModel);
                consumption.Id = Guid.NewGuid();

                ServiceFactory    serviceFactory      = new ServiceFactory(this._faciTechDbContext, this._contactId);
                ConsumptionImport communityOnboarding = new ConsumptionImport(serviceFactory, communityId, outputFilePath);
                consumption.CommunityId = communityId;
                var isValid = communityOnboarding.Import(consumption, byteArray);

                FileManager fileManager = new FileManager(outputFilePath);
                var         outputFile  = new Attachment()
                {
                    FileName    = fileName,
                    FileContent = fileManager.GetByteArray(),
                    TableName   = "Consumption",
                    TableId     = consumption.Id,
                    MimeType    = consumptionApiModel.Attachment.ContentType
                };

                _attachmentService.Create(outputFile, true);
                fileUploadManager.DeleteFile();
                return(Ok(new ApiResponse(new { Id = consumption.Id, IsAllValid = isValid, FileName = fileName }, ApiStatus.Success)));
            }
            catch (Exception ex)
            {
                return(new UnknownErrorResult(ex, base._errorEnabled));
            }
        }
        public Task <bool> DeleteAttachment(int id)
        {
            var fileName = _db.Documents.Include(x => x.Task)
                           .Where(x => x.Id == id)
                           .AsNoTracking()                  //useful when the results are used in a read-only scenario
                           .FirstOrDefault();

            if (fileName != null)
            {
                //delete from database
                var file = new Documents {
                    Id = id
                };
                _db.Remove(file);
                _db.SaveChanges();

                //delete actual file
                _fileManager.DeleteFile(fileName.Task.ProjectId, fileName.FileName);

                return(System.Threading.Tasks.Task.FromResult(true));
            }

            return(System.Threading.Tasks.Task.FromResult(false));
        }
        public ActionResult DeleteFile(string fileName)
        {
            FileUploadManager.DeleteFile(fileName);

            return(new JsonResult());
        }