public ActionResult Create(NewsViewModel newsViewModel)
        {
            News news = new News();

            if (ModelState.IsValid)
            {
                if (newsViewModel.Photo != null)
                {
                    ImageUploadService imageUploadService = new ImageUploadService();
                    bool isImageValid = imageUploadService.CheckImageExtension(newsViewModel);
                    if (isImageValid == true)
                    {
                        string fileName = imageUploadService.UploadImage(newsViewModel);
                        news.Photo = fileName;
                    }
                    else
                    {
                        ModelState.AddModelError("PhotoContentTypeError", "The file you are trying to upload is not an image!");
                        return(View(newsViewModel));
                    }
                }
                news.ID      = newsViewModel.ID;
                news.Title   = newsViewModel.Title;
                news.Content = newsViewModel.Content;
                List <Users> allUsers    = UnitOfWork.UOW.UserRepository.GetAll();
                Users        currentUser = allUsers.Where(x => x.ID == SessionDTO.ID).FirstOrDefault();
                news.User = currentUser;
                return(base.Create(news));
            }
            else
            {
                return(View(newsViewModel));
            }
        }
Beispiel #2
0
        private static async Task <HttpResponseMessage> ResizeMultiple(HttpRequestMessage request, InputImageParameters inputParameters, IReadOnlyCollection <ImageSizeParam> imageSizes, ILogger log)
        {
            List <ImageResizeResultModel> resizeResults = new List <ImageResizeResultModel>();
            List <Task> uploadTasks = new List <Task>();

            using (await _imageResizeService.CaptureAsync())
            {
                try
                {
                    using (MemoryStream imageStreamCopy = new MemoryStream())
                    {
                        Stream imageStream = await request.Content.ReadAsStreamAsync();

                        await imageStream.CopyToAsync(imageStreamCopy);

                        if (imageStreamCopy.Length == 0)
                        {
                            return(request.CreateResponse(HttpStatusCode.BadRequest, new { Body = new { Image = "Required" } }));
                        }

                        imageStreamCopy.Position    = 0;
                        inputParameters.InputStream = imageStreamCopy;

                        foreach (OutputImageParameters image in _imageResizeService.ResizeMultiple(inputParameters, imageSizes))
                        {
                            resizeResults.Add(new ImageResizeResultModel(image.Width, image.Height, image.Size, image.Quality));
                            uploadTasks.Add(_imageUploadService.UploadImage(inputParameters.UploadUrl, inputParameters.OutputContentType, image));
                        }
                    }
                }
                catch (ArgumentException ex)
                {
                    object validationData = ex.Data["ValidationData"];
                    if (validationData != null)
                    {
                        return(request.CreateResponse(HttpStatusCode.BadRequest, validationData));
                    }

                    throw;
                }
            }

            try
            {
                await Task.WhenAll(uploadTasks);
            }
            catch (HttpRequestException ex)
            {
                log.LogError(ex, "Upload requests failed");
                request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
            }

            HttpResponseMessage response = request.CreateResponse(
                HttpStatusCode.OK,
                resizeResults,
                JsonMediaTypeFormatter.DefaultMediaType
                );

            return(response);
        }
Beispiel #3
0
        public Dictionary <string, string> UploadImage()
        {
            var obj = new Dictionary <string, string> {
                ["reason"] = "An unknown error occurred."
            };

            // If above 4MB, refuse it (actually a bit more to be generous)
            if (Request.ContentLength > 4200000)
            {
                obj["reason"]       = "Uploads must not exceed 4MB.";
                Response.StatusCode = 403;
                return(obj);
            }

            // If not a seller, refuse it
            if (_accountService.ValidateSellerSession(Request.Cookies, new DbContext()) == null)
            {
                obj["reason"]       = "Please log in as a seller to use this form.";
                Response.StatusCode = 403;
                return(obj);
            }

            const string baseUrl = "https://studio1btask.blob.core.windows.net/images/";
            var          blob    = Request.Body;

            var fileName = Guid.NewGuid() + ".jpg";

            _imageUploadService.UploadImage(fileName, blob).Wait();
            obj["url"] = baseUrl + fileName;
            return(obj);
        }
Beispiel #4
0
 private void UpdateButton_Click(object sender, EventArgs e)
 {
     sLocalFile           = CreateBitmap(UserStatusText.Text);
     Description          = "STATUS|" + UserStatusText.Text;
     progressBar1.Visible = true;
     ImageUploadService.UploadImage(sLocalFile,
                                    Client_UploadFileCompleted, Client_UploadProgressChanged, false);
 }
Beispiel #5
0
 private void UploadButton_Click(object sender, EventArgs e)
 {
     ImageUploadService.UploadImage(sLocalFile,
                                    Client_UploadFileCompleted, Client_UploadProgressChanged, HDBox.Checked);
 }