示例#1
0
        public async Task <IActionResult> Upload(string id)
        {
            var tokenString = Request.Headers["Authorization"].ToString().Remove(0, 7);
            var handler     = new JwtSecurityTokenHandler();
            var jsonToken   = handler.ReadToken(tokenString);
            var token       = handler.ReadToken(tokenString) as JwtSecurityToken;
            var isPasswordChangeRequired = Convert.ToBoolean(token.Claims.First(claim => claim.Type == "password_change_required").Value);

            if (isPasswordChangeRequired)
            {
                return(Forbid());
            }

            try
            {
                int         filesCount = Request.Form.Files.Count;
                IFormFile[] files      = new IFormFile[filesCount];
                string[]    names      = new string[filesCount];
                for (int i = 0; i < filesCount; i++)
                {
                    names[i] = Request.Form.Files[i].FileName;
                    files[i] = Request.Form.Files[i];
                }
                await _attachmentService.AddAttachments(files, id);

                return(Ok(new { message = "Upload successful" }));
            }
            catch (Exception ex)
            {
                return(BadRequest(new { message = "Upload failed: " + ex.Message }));
            }
        }