public JsonResult UploadImageToDB() { try { ImageViewModel uploadImageViewModel = new ImageViewModel(); int uploadImageId = 0; var r = new List <ImageViewModel>(); if (Request.Files == null) { throw new ArgumentNullException("Files cannot be null"); } foreach (string file in Request.Files) { HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase; if (hpf.ContentLength == 0) { continue; } Application.Services.Abstract.DTO.IImage image = new Application.Services.DTO.Image(); image.UserImage = uploadImageViewModel.UserImage = ImageHelper.Get(hpf); uploadImageId = _peopleService.UploadImage(image); uploadImageViewModel.UploadedImageId = uploadImageId; uploadImageViewModel.Name = hpf.FileName; } return(Json(uploadImageViewModel, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { _logger.Error(ex); throw; } }
public ImageJSON Upload() { try { MultipartFormDataParser parser = new MultipartFormDataParser(HttpContext.Current.Request.InputStream); ImageJSON imageJson = new ImageJSON(); int uploadImageId = 0; // Single file access: FilePart file = parser.Files.First(); string filename = file.FileName; if (file != null && file.Data != null) { // Save the file to db Application.Services.Abstract.DTO.IImage image = new Application.Services.DTO.Image(); image.UserImage = parser.ReadFully(file.Data); uploadImageId = _imageService.UploadImage(image); imageJson.UploadedImageId = uploadImageId; } else { throw new WebException("The posted file was not recognised.", WebExceptionStatus.SendFailure); } return(imageJson); } catch (Exception ex) { _logger.Error(ex); throw ex; } }
public ImageJSON Save(FacebookJSON facebookJson) { int uploadImageId = 0; Application.Services.Abstract.DTO.IImage image; try { WebRequest request = WebRequest.Create(facebookJson.ImageUrl); WebResponse response = request.GetResponse(); using (var stream = response.GetResponseStream()) { if (stream == null) { return(null); } image = new Application.Services.DTO.Image() { UserImage = ImageSericeHelper.Get(stream) }; } //// Save the file to db uploadImageId = _imageService.UploadImage(image); } catch (Exception ex) { _logger.Error(ex); throw; } return(new ImageJSON() { UploadedImageId = uploadImageId }); }