public async Task <ImageResponseModel> UploadOne(IFormFile file, ImageType type) { Validate(file); // Define image uploading sizes var uploadingSize = GetSize(type); var response = new ImageResponseModel(); try { using (var fileStream = file.OpenReadStream()) { var fileInfo = SixLabors.ImageSharp.Image.Identify(fileStream); // Get image compact size var compactSize = GetCompactImageSize(fileInfo.Width, fileInfo.Height, type, file.FileName); response = await Save(fileStream, file.FileName, uploadingSize, compactSize); } return(response); } catch (Exception ex) { throw new CustomException(HttpStatusCode.BadRequest, "file", ex.InnerException?.Message ?? ex.Message); } }
public IHttpActionResult UploadImage() { var file = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null; if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName).Replace(" ", ""); string folderPath = HttpContext.Current.Server.MapPath("~/Content/Uploads/Images/"); string relativePath = "/Content/Uploads/Images/" + fileName; var fullPath = Path.Combine(folderPath, fileName); if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } file.SaveAs(fullPath); ImageResponseModel ResponseMessage = new ImageResponseModel() { imageUrl = relativePath }; return(Json(ResponseMessage)); } return(null); }
public ActionResult SetImgageFile() { try { System.IO.FileInfo fi = new System.IO.FileInfo(Request.Files["file"].FileName); string FileName = fi.Name; var bytes = new byte[Request.Files["file"].InputStream.Length]; Request.Files["file"].InputStream.Read(bytes, 0, bytes.Length); var fileID = FileModelReg.Insert(FileName, bytes); //string filePath = ParsePath(fileName); var sendObj = new ImageResponseModel { FileKey = fileID.ToString(), FileName = FileName, FileUrl = Url.RouteUrl("DownloadURL", new { controller = "ImageMng", action = "GetImageFile", FileID = fileID.ToString(), FileName = FileName }), FileUrlThumb = Url.RouteUrl("DownloadURL", new { controller = "ImageMng", action = "GetImageFile", FileID = fileID.ToString(), FileName = FileName }), }; return(View("FileUploaded", sendObj)); } catch (Exception) { var sendObj = new ImageResponseModel { IsFail = true }; return(View("FileUploaded", sendObj)); } }
public ImageResponseModel AddImage(ImageRequestModel image) { var imgId = _imageService.AddImage(image.name, image.type, image.data); var imgResponse = new ImageResponseModel { imageId = imgId }; return(imgResponse); }
public IHttpActionResult GetImageResponse(ImageResponseModel objectvalue) { // return Ok(objectvalue.predictions[0].tagName.ToString()); var json = new JavaScriptSerializer().Serialize(objectvalue); string connectionString = @"mongodb://*****:*****@salesapp-pwcindia.documents.azure.com:10255/?ssl=true&replicaSet=globaldb"; MongoClientSettings settings = MongoClientSettings.FromUrl( new MongoUrl(connectionString) ); settings.SslSettings = new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 }; settings.MaxConnectionIdleTime = TimeSpan.FromSeconds(40); var mongoClient = new MongoClient(settings); IMongoDatabase db = mongoClient.GetDatabase("SalesApp"); var imageDetails = db.GetCollection <BsonDocument>("DealerAssessment"); BsonElement imageElement = new BsonElement("Dealer ID", "A003"); BsonElement imageResponseData = new BsonElement("Response Data", json.ToString()); BsonDocument imageDoc = new BsonDocument { imageElement, imageResponseData }; imageDetails.InsertOne(imageDoc); int Total = objectvalue.predictions.Select(x => x.tagId.Count()).Count(); int TagCount = objectvalue.predictions.Where(x => x.tagName == "CocaCola") .Select(x => x.tagId.Count()).Count(); decimal result = Math.Round((decimal)TagCount / Total * 100, 2); return(Ok(result.ToString())); }
public ActionResult SetLayerIconFile() { System.IO.FileInfo fi = new System.IO.FileInfo(Request.Files["file"].FileName); string FileName = fi.Name; var bytes = new byte[Request.Files["file"].InputStream.Length]; Request.Files["file"].InputStream.Read(bytes, 0, bytes.Length); using (System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes)) { var img = new System.Drawing.Bitmap(ms); var newBytes = ResizeImage(img, 32, 32); if (newBytes != null) { bytes = newBytes; } img.Dispose(); var idx = FileName.LastIndexOf("."); if (idx > 0) { FileName = FileName.Substring(0, idx) + ".jpg"; } } var fileID = FileModelReg.Insert(FileName, bytes); //string filePath = ParsePath(fileName); var sendObj = new ImageResponseModel { FileKey = fileID.ToString(), FileName = FileName, FileUrl = Url.RouteUrl("DownloadURL", new { controller = "ImageMng", action = "GetImageFile", FileID = fileID.ToString(), FileName = FileName }), FileUrlThumb = Url.RouteUrl("DownloadURL", new { controller = "ImageMng", action = "GetImageFile", FileID = fileID.ToString(), FileName = FileName }), }; return(View("FileUploaded", sendObj)); }
public async Task <List <MultipleImagesResponseModel> > UploadMultipleSavingValid(List <IFormFile> images, ImageType type) { try { // Define image uploading sizes var uploadingSize = GetSize(type); var result = new List <MultipleImagesResponseModel>(); // If image doesn`t pass at least one check - change status and pass to next iteration foreach (var y in images) { // Add name to show wich image incorrect var data = new MultipleImagesResponseModel { Name = y.FileName }; result.Add(data); var extention = Path.GetExtension(y.FileName).ToLower(); if (y.Length > MAX_FILE_SIZE) { data.Status = ImageSaveStatus.InvalidLength; continue; } if (!EXTENTIONS.Contains(extention)) { data.Status = ImageSaveStatus.InvalidFormat; continue; } var response = new ImageResponseModel(); using (var fileStream = y.OpenReadStream()) { var fileInfo = SixLabors.ImageSharp.Image.Identify(fileStream); // Restriction for server correct work if (fileInfo.Width > MAX_IMAGE_SIDE || fileInfo.Height > MAX_IMAGE_SIDE) { data.Status = ImageSaveStatus.InvalidSize; continue; } // Set default image dimention var compactSize = new SixLabors.Primitives.Size { Height = fileInfo.Height, Width = fileInfo.Width }; switch (type) { case ImageType.Square: if (fileInfo.Width < MIN_IMAGE_WIDTH || fileInfo.Height < MIN_IMAGE_HEIGHT) { data.Status = ImageSaveStatus.InvalidDimension; continue; } if (Math.Abs(fileInfo.Width - fileInfo.Height) > 0) { data.Status = ImageSaveStatus.InvalidDimension; continue; } if (fileInfo.Width > IMAGE_COMPACT_WIDTH || fileInfo.Height > IMAGE_COMPACT_HEIGHT) { compactSize.Width = IMAGE_COMPACT_WIDTH; compactSize.Height = IMAGE_COMPACT_HEIGHT; } break; case ImageType.Normal: if (fileInfo.Height < MIN_IMAGE_HEIGHT || fileInfo.Width < MIN_IMAGE_WIDTH) { data.Status = ImageSaveStatus.InvalidDimension; continue; } // Passing zero for one of height or width will automatically preserve the aspect ratio of the original image if (fileInfo.Width > IMAGE_COMPACT_WIDTH || fileInfo.Height > IMAGE_COMPACT_HEIGHT) { if (fileInfo.Width > fileInfo.Height) { compactSize.Width = IMAGE_COMPACT_WIDTH; compactSize.Height = 0; } else { compactSize.Height = IMAGE_COMPACT_HEIGHT; compactSize.Width = 0; } } break; } response = await Save(fileStream, y.FileName, uploadingSize, compactSize); // In case of succes change status and return image response model data.Status = ImageSaveStatus.Saved; data.Image = _mapper.Map <ImageResponseModel>(response); } } // Throw an exception there are no valid images if (result.Any(x => x.Status == ImageSaveStatus.Saved)) { return(result); } else { throw new CustomException(HttpStatusCode.BadRequest, "images", "No image has been saved"); } } catch (Exception ex) { throw new CustomException(HttpStatusCode.BadRequest, "images", ex.InnerException?.Message ?? ex.Message); } }