コード例 #1
0
        public async Task <Result <FileModel> > UploadFile()
        {
            var form = await Request.ReadFormAsync();

            int?   bugTicketId        = null;
            int?   bugTicketCommentId = null;
            string publicId           = null;
            string description        = String.Empty;

            if (!StringValues.IsNullOrEmpty(form["bugTicketId"]))
            {
                bugTicketId = Int32.Parse(form["bugTicketId"]);
            }
            if (!StringValues.IsNullOrEmpty(form["bugTicketCommentId"]))
            {
                bugTicketCommentId = Int32.Parse(form["bugTicketCommentId"]);
            }
            if (!StringValues.IsNullOrEmpty(form["publicId"]))
            {
                publicId = form["publicId"];
            }

            var parsedContentDisposition = ContentDispositionHeaderValue.Parse(form.Files[0].ContentDisposition);

            var contentType = form.Files[0].ContentType;

            using (var stream = form.Files[0].OpenReadStream())
            {
                var fileContent = stream.ReadFully();
                try
                {
                    BugFile file     = null;
                    var     mode     = bugTicketId.HasValue ? BugFileType.Ticket : BugFileType.Comment;
                    var     fileName = await _helpService.UploadBugFileToStoreAsync(mode, fileContent, parsedContentDisposition.FileName.Replace("\"", ""), publicId, contentType);

                    if (!String.IsNullOrEmpty(fileName) && ((bugTicketId.HasValue && bugTicketId != 0) || (bugTicketCommentId.HasValue && bugTicketCommentId != 0)))
                    {
                        file = new BugFile
                        {
                            IdBugTicket        = bugTicketId,
                            IdBugTicketComment = bugTicketCommentId,
                            FileName           = fileName,
                            Description        = description
                        };

                        file = await _helpService.AddBugFileAsync(file);
                    }

                    var uploadDate = file?.UploadDate ?? DateTime.Now;
                    var id         = file?.Id ?? 0;
                    return(new FileModel()
                    {
                        FileName = fileName, UploadDate = uploadDate, Id = id
                    });
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.ToString());
                    throw;
                }
            }
        }