Example #1
0
        ///// <summary>
        ///// 根据文件路径,删除文件
        ///// </summary>
        ///// <param name="filePath"></param>
        ///// <returns></returns>
        //public bool DeleteFile(string filePath)
        //{
        //    if (string.IsNullOrWhiteSpace(filePath))
        //    {
        //        return false;
        //    }
        //    return GetFileService().Delete(filePath);
        //}

        /// <summary>
        /// 根据Fid,删除文件
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public bool DeleteFileByFid(string fid)
        {
            if (string.IsNullOrWhiteSpace(fid))
            {
                return(false);
            }

            FapAttachment attachment = _dataAccessor.Get <FapAttachment>(fid);

            if (attachment == null)
            {
                return(false);
            }

            try
            {
                FapFileInfo fileInfo = new FapFileInfo();
                fileInfo.FilePath = attachment.FilePath;
                fileInfo.FileId   = attachment.Id;

                string fileServiceType = attachment.SaveModel;
                bool   deleted         = GetFileService(fileServiceType).Delete(fileInfo);
                if (deleted)
                {
                    _dataAccessor.DeleteExec("FapAttachment", "Id=" + attachment.Id);
                }
                return(true);
            }
            catch (Exception ex)
            {
                _logger.CreateLogger <FapFileService>().LogError(ex.Message);
            }

            return(false);
        }
        /// <summary>
        /// 上传附件
        /// UploadExtraData传来的参数会采用Request.Form["param"]接收
        /// </summary>
        /// <param name="id">用户的ID</param>
        /// <returns></returns>
        public JsonResult UploadFile(string fid)
        {
            Guard.Against.NullOrWhiteSpace(fid, nameof(fid));
            try
            {
                var files = Request.Form.Files;
                if (files != null && files.Count > 0)
                {
                    foreach (var file in files)
                    {
                        FapAttachment attachment = new FapAttachment();
                        attachment.Bid      = fid;
                        attachment.FileName = file.FileName;
                        attachment.FileType = file.ContentType;
                        string attFid = _fapFileService.UploadFile(file.OpenReadStream(), attachment);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(Json(ResponseViewModelUtils.Failure("出现错误")));
            }

            return(Json(ResponseViewModelUtils.Sueecss()));
        }
Example #3
0
        public Stream Download(FapFileInfo fileInfo)
        {
            FapAttachment attachment = _dataAccessor.Get <FapAttachment>((int)fileInfo.FileId);

            if (attachment != null && attachment.FileContent != null)
            {
                return(new MemoryStream(attachment.FileContent));
            }
            return(null);
        }
Example #4
0
        public Stream GetFileStream(FapAttachment attachment)
        {
            FapFileInfo fileInfo = new FapFileInfo();

            fileInfo.FilePath = attachment.FilePath;
            fileInfo.FileId   = attachment.Id;

            string fileServiceType = attachment.SaveModel;

            return(GetFileService(fileServiceType).Download(fileInfo));
        }
        /// <summary>
        /// 查看附件图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public FileResult AttachmentImg(string fid)
        {
            FapAttachment attachment = _fapFileService.DownloadFileByFid(fid, out Stream strm);

            if (attachment == null || strm == null)
            {
                return(File("~/Content/avatars/profile-pic.jpg", "image/png"));
            }
            else
            {
                return(new FileStreamResult(strm, attachment.FileType));
            }
        }
        /// <summary>
        /// 下载附件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult DownloadFile(string fid)
        {
            FapAttachment attachment = _fapFileService.DownloadFileByFid(fid, out Stream strm);

            if (attachment == null || strm == null)
            {
                return(Content("服务器未找到文件"));
                //throw new FileNotFoundException("服务器未找到文件");
            }
            else
            {
                return(File(strm, attachment.FileType, attachment.FileName));
            }
        }
 public JsonResult UploadFile(string fid)
 {
     Guard.Against.NullOrWhiteSpace(fid, nameof(fid));
     try
     {
         var files = Request.Form.Files;
         if (files != null && files.Count > 0)
         {
             if (fid.EqualsWithIgnoreCase("0"))
             {
                 fid = UUIDUtils.Fid;
             }
             var           file       = files[0];
             FapAttachment attachment = new FapAttachment();
             attachment.Bid      = fid;
             attachment.FileName = file.FileName;
             attachment.FileType = file.ContentType;
             //using Image myImage = Image.FromStream(file.OpenReadStream());
             ////获得图片宽和高
             //int widths = myImage.Width;
             //int heights = myImage.Height;
             //using var stream = new MemoryStream();
             //myImage.Save(stream, ImageFormat.Png);
             //stream.Position = 0;
             //string attFid = _fapFileService.UploadFile(stream, attachment);
             string attFid = _fapFileService.UploadFile(file.OpenReadStream(), attachment);
             return(Json(new
             {
                 status = 0,
                 img_url = _applicationContext.BaseUrl + "/Component/Photo/" + fid,
                 img_id = attFid,
                 width = 0,
                 height = 0
             }));
         }
     }
     catch (Exception ex)
     {
         _logger.LogError(ex.Message);
     }
     return(Json(new
     {
         status = 1,
         img_url = ""
     }));
 }
        public FileResult Photo(string fid)
        {
            if (fid.IsMissing())
            {
                return(File("~/Content/avatars/profile-pic.jpg", "image/png"));
            }
            FapAttachment attachment = _fapFileService.DownloadOneFileByBid(fid, out Stream stream);

            if (attachment == null || stream == null)
            {
                return(File("~/Content/avatars/profile-pic.jpg", "image/png"));
            }
            else
            {
                return(File(stream, attachment.FileType));
            }
        }
Example #9
0
        ///// <summary>
        ///// 根据文件路径,下载文件
        ///// </summary>
        ///// <param name="filePath"></param>
        ///// <returns></returns>
        //public Stream DownloadFile(string filePath)
        //{
        //    if (string.IsNullOrWhiteSpace(filePath))
        //    {
        //        return null;
        //    }
        //    return GetFileService().Download(filePath);
        //}

        /// <summary>
        /// 根据Fid,下载文件
        /// </summary>
        /// <param name="fid"></param>
        /// <returns>返回的是文件流</returns>
        public FapAttachment DownloadFileByFid(string fid, out Stream stream)
        {
            stream = null;
            if (string.IsNullOrWhiteSpace(fid))
            {
                return(null);
            }

            FapAttachment attachment = _dataAccessor.Get <FapAttachment>(fid);

            if (attachment == null)
            {
                return(null);
            }
            stream = this.GetFileStream(attachment);
            return(attachment);
        }
Example #10
0
        public bool Upload(Stream stream, FapFileInfo fileInfo, FileUploadEventHandler updateEvent)
        {
            FapAttachment attachment = _dataAccessor.Get <FapAttachment>((int)fileInfo.FileId);

            if (attachment != null)
            {
                using (var inStream = stream)
                {
                    byte[] bytes = new byte[inStream.Length];
                    inStream.Read(bytes, 0, bytes.Length);
                    attachment.FileContent = bytes;
                }

                _dataAccessor.Update <FapAttachment>(attachment);
            }

            return(true);
        }
Example #11
0
        public string AttachmentProcess()
        {
            try
            {
                var files = Request.Form.Files;
                if (files != null && files.Count > 0)
                {
                    string            fileName = files[0].FileName;
                    string[]          names    = fileName.Split('_');
                    string            empCode  = names[0];
                    DynamicParameters param    = new DynamicParameters();
                    param.Add("EmpCode", empCode);
                    Fap.Core.Rbac.Model.Employee employee = _dbContext.QueryFirstOrDefaultWhere <Fap.Core.Rbac.Model.Employee>("EmpCode=@EmpCode", param);
                    if (employee == null)
                    {
                        return("0");
                    }

                    if (string.IsNullOrWhiteSpace(employee.EmpPhoto))
                    {
                        employee.EmpPhoto = UUIDUtils.Fid;
                        string sql = "update Employee set EmpPhoto=@EmpPhoto where id=@Id";
                        _dbContext.Execute(sql, new DynamicParameters(new { EmpPhoto = employee.EmpPhoto, Id = employee.Id }));
                    }
                    FapAttachment attachment = new FapAttachment();

                    attachment.Bid      = employee.EmpPhoto;
                    attachment.FileName = files[0].FileName;
                    attachment.FileType = files[0].ContentType;
                    string           path = SysIO.Path.Combine(Environment.CurrentDirectory, "temp", Guid.NewGuid().ToString());
                    SysIO.FileStream fs   = SysIO.File.Create(path);

                    files[0].CopyTo(fs);
                    string attFid = _fileService.UploadFile(fs, attachment);
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }

            return("1");
        }
Example #12
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="attachement"></param>
        /// <returns></returns>
        public string UploadFile(Stream stream, FapAttachment attachement)
        {
            if (stream == null || attachement == null)
            {
                return("");
            }

            FapFileInfo fileInfo = new FapFileInfo();

            fileInfo.FileSize = (attachement.FileSize == 0 ? (stream.Length / 1024) : attachement.FileSize);
            fileInfo.FileName = attachement.FileName;
            //fileInfo.FileId = ""+attachement.Id;
            fileInfo.FileSuffix = (string.IsNullOrWhiteSpace(attachement.FileExtension) ? Path.GetExtension(attachement.FileName) : attachement.FileExtension);

            attachement.FileSize      = fileInfo.FileSize;
            attachement.FileExtension = fileInfo.FileSuffix;
            attachement.SaveModel     = this._fileServiceType;

            long id = _dataAccessor.Insert <FapAttachment>(attachement);

            fileInfo.FileId = id;

            bool success = GetFileService().Upload(stream, fileInfo, (a, b) =>
            {
                fileInfo.FilePath = b.FileFullName;
            });

            if (success)
            {
                //newAttachment.FilePath = fileInfo.FilePath;
                //dataAccessor.EUpdate<FapAttachment>(attachement, false);
                _dataAccessor.Execute("update FapAttachment set FilePath=@FilePath where Id=@Id", new DynamicParameters(new { FilePath = fileInfo.FilePath, Id = id }));
            }
            else
            {
                _dataAccessor.Delete <FapAttachment>(attachement);
            }

            return(attachement.Fid);
        }
Example #13
0
        public string AttachmentProcess()
        {
            try
            {
                var files = Request.Form.Files;
                if (files != null && files.Count > 0)
                {
                    string            fileName = files[0].FileName;
                    string[]          names    = fileName.Split('_');
                    string            empCode  = names[0];
                    DynamicParameters param    = new DynamicParameters();
                    param.Add("EmpCode", empCode);
                    Core.Rbac.Model.Employee employee = _dbContext.QueryFirstOrDefault <Core.Rbac.Model.Employee>($"select Id, EmpPhoto from Employee where EmpCode=@EmpCode", param);
                    if (employee == null)
                    {
                        return("0");
                    }
                    if (employee.EmpPhoto.IsMissing())
                    {
                        employee.EmpPhoto = UUIDUtils.Fid;
                        string sql = "update Employee set EmpPhoto=@EmpPhoto where Id=@Id";
                        _dbContext.Execute(sql, new DynamicParameters(new { employee.EmpPhoto, employee.Id }));
                    }
                    FapAttachment attachment = new FapAttachment();
                    attachment.Bid      = employee.EmpPhoto;
                    attachment.FileName = files[0].FileName;
                    attachment.FileType = files[0].ContentType;
                    string attFid = _fileService.UploadFile(files[0].OpenReadStream(), attachment);
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }

            return("1");
        }
Example #14
0
        public string ParseAttachment(MimeMessage message)
        {
            string uuid = UUIDUtils.Fid;
            //解析附件
            // Build up the attachment list
            var attachments = message.Attachments;

            if (attachments != null && attachments.Any())
            {
                //FapFileServiceHelper fh = new FapFileServiceHelper();
                foreach (MimePart attachmentPart in attachments)
                {
                    if (attachmentPart.IsAttachment)
                    {
                        FapAttachment attachment = new FapAttachment();
                        attachment.Bid      = uuid;
                        attachment.FileName = attachmentPart.ContentDisposition.FileName;
                        attachment.FileType = attachmentPart.ContentType.MimeType;
                        if (attachment != null)
                        {
                            using (var cancel = new Sys.Threading.CancellationTokenSource())
                            {
                                string filePath = Path.Combine(Sys.Environment.CurrentDirectory, attachment.FileName);
                                using (Stream stream = new MemoryStream())
                                {
                                    attachmentPart.Content.DecodeTo(stream, cancel.Token);
                                    stream.Seek(0, SeekOrigin.Begin);
                                    string attFid = _fapFileService.UploadFile(stream, attachment);
                                }
                            }
                        }
                    }
                }
            }
            return(uuid);
        }