Beispiel #1
0
        public async Task <IHttpActionResult> UploadAvatar()
        {
            var httpRequest = HttpContext.Current.Request;

            if (httpRequest.Files.Count != 1)
            {
                return(Ok(new MessageData(false, "请选择一张图片上传")));
            }
            #region Field

            //文件流
            var postFile         = httpRequest.Files[0];
            var uploadMaxSetting = _settingService.FindSettingByName("上传文件最大值(M)");
            int maxContentLength = 1024 * 1024 * Convert.ToInt16(uploadMaxSetting.Number);
            int authUserId       = int.Parse(User.Identity.GetUserId());
            var authUser         = await _userService.FindByIdAsync(authUserId);

            var apiHost = _settingService.FindSettingByName("api物理地址");

            #endregion
            //允许上传的格式
            IList <string> AllowedFileExtensions = new List <string> {
                ".jpg", ".gif", ".png"
            };
            var ext       = postFile.FileName.Substring(postFile.FileName.LastIndexOf('.'));
            var extension = ext.ToLower();
            if (!AllowedFileExtensions.Contains(extension))
            {
                var message = string.Format("请上传 .jpg,.gif,.png格式的图片");
                return(Ok(new MessageData(false, message)));
            }
            //图片大小限制
            if (postFile.ContentLength > maxContentLength)
            {
                return(Ok(new MessageData(false, "文件上传最大为" + Convert.ToInt16(uploadMaxSetting.Number) + " M")));
            }
            Attachment imgEntity;
            try
            {
                imgEntity              = new Attachment();
                imgEntity.CreatedTime  = DateTime.Now;
                imgEntity.ContentType  = postFile.ContentType;
                imgEntity.FileName     = postFile.FileName;
                imgEntity.OriginalName = StringHelper.GenerateOriginName(postFile.FileName, imgEntity.CreatedTime);
                imgEntity.FileLength   = postFile.ContentLength;
                imgEntity.TenantType   = UserType.Customer;
                imgEntity.UserId       = authUserId;

                _attachmentService.Create(imgEntity, postFile.InputStream);
                authUser.AvatarId = imgEntity.Id;
                await _userService.UpdateAsync(authUser);
            }
            catch (Exception ex)
            {
                return(Ok(new MessageData(false, "上传头像失败")));
            }

            return(Ok(new MessageData(true, "上传头像成功", new { avatarPath = apiHost.Code + imgEntity.FilePath })));
        }
Beispiel #2
0
        public IActionResult UploadCV(IFormFile CV)
        {
            if (CV == null)
            {
                return(View("FileIsNull"));
            }

            //On Success redirecct to the success page
            var cvName       = SaveToStorage(this.CV, CV);
            var cvAttachment = new Attachment
            {
                Id             = Guid.NewGuid().ToString(),
                AttachmentType = "CV",
                JobSeekerId    = HttpContext.Session.Get <string>("JobSeekerId"),
                AttachmentName = cvName,
                IsActive       = true
            };

            //Call a service for uploading a CV
            attachmentService.Create(cvAttachment);
            return(View("CvUploadSuccess"));
        }
        public async Task <IActionResult> Create(AttachmentDTO attachment)
        {
            var Id = await _attachment.Create(attachment);

            return(Ok(Id));
        }