Пример #1
0
        public async Task <IActionResult> UploadFile([FromForm] PhantomThreadItem thread)
        {
            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                return(BadRequest($"Expected a multipart request, but got {Request.ContentType}"));
            }
            try
            {
                using (var stream = thread.Image.OpenReadStream())
                {
                    var cloudBlock = await UploadToBlob(thread.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."));
                    }

                    PhantomThread phantomThread = new PhantomThread();
                    phantomThread.Title   = thread.Title;
                    phantomThread.Content = thread.Content;
                    phantomThread.User    = thread.User;

                    System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
                    phantomThread.Height = image.Height.ToString();
                    phantomThread.Width  = image.Width.ToString();
                    phantomThread.Url    = cloudBlock.SnapshotQualifiedUri.AbsoluteUri;

                    System.Globalization.CultureInfo.CurrentCulture.ClearCachedData();
                    var myTimeZone      = TimeZoneInfo.FindSystemTimeZoneById("New Zealand Standard Time");
                    var currentDateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, myTimeZone);
                    phantomThread.Uploaded = currentDateTime.ToString();

                    _context.PhantomThread.Add(phantomThread);
                    await _context.SaveChangesAsync();

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