コード例 #1
0
        public async Task <IActionResult> UploadFile([FromForm] BookForm book)
        {
            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                return(BadRequest($"Expected a multipart request, but got {Request.ContentType}"));
            }
            try
            {
                using (var stream = book.Image.OpenReadStream())
                {
                    var cloudBlock = await UploadToBlob(book.Image.FileName, null, stream);

                    //// Retrieve the filename of the file you have uploaded
                    //var filename = provider.FileData.FirstOrDefault()?.LocalFileName;
                    if (string.IsNullOrEmpty(cloudBlock.StorageUri.ToString()))
                    {
                        return(BadRequest("An error has occured while uploading your file. Please try again."));
                    }
                    //BookTag tagsList = new

                    Book bookItem = new Book()
                    {
                        Created       = DateTime.Now,
                        Title         = book.Title,
                        CoverUrl      = cloudBlock.SnapshotQualifiedUri.AbsoluteUri,
                        Author        = book.Author,
                        ISBN          = book.ISBN,
                        YearPublished = book.YearPublished,
                        BookRating    = book.BookRating,
                        BookReview    = book.BookReview,
                        BookTags      = _bookService.ParseTags(book.tags)
                    };

                    await _bookService.AddBookAsync(bookItem);

                    return(Ok($"File: {book.Title} has successfully uploaded"));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest($"An error has occured. Details: {ex.Message}"));
            }
        }