public IHttpActionResult UploadPicture() { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } var files = HttpContext.Current.Request.Files; // check if files are on the request.*/ if (files.Count == 0) { return(Json(new { Success = false })); } try { var newImages = new List <object>(); for (var index = 0; index < files.Count; index++) { //the file var file = files[index]; //and it's name var fileName = file.FileName; //stream to read the bytes var stream = file.InputStream; var pictureBytes = new byte[stream.Length]; stream.Read(pictureBytes, 0, pictureBytes.Length); //file extension and it's type var fileExtension = Path.GetExtension(fileName); if (!string.IsNullOrEmpty(fileExtension)) { fileExtension = fileExtension.ToLowerInvariant(); } var contentType = file.ContentType; if (string.IsNullOrEmpty(contentType)) { contentType = PictureUtility.GetContentType(fileExtension); } //save the picture now var picture = _pictureService.InsertPicture(pictureBytes, contentType, null); newImages.Add(new { ImageUrl = _pictureService.GetPictureUrl(picture.Id), ImageId = picture.Id }); } return(Json(new { Success = true, Images = newImages })); } catch (Exception e) { return(Json(new { Success = false })); } }
public IHttpActionResult UploadPicture(int artistPageId, IEnumerable <HttpPostedFileBase> file) { //first get artist page var artistPage = _artistPageService.Get(artistPageId); if (!CanEdit(artistPage)) { return(Response(new { Success = false, Message = "Unauthorized" })); } var files = file.ToList(); var newImageUrl = ""; foreach (var fi in files) { Stream stream = null; var fileName = ""; var contentType = ""; if (file == null) { throw new ArgumentException("No file uploaded"); } stream = fi.InputStream; fileName = Path.GetFileName(fi.FileName); contentType = fi.ContentType; var fileBinary = new byte[stream.Length]; stream.Read(fileBinary, 0, fileBinary.Length); var fileExtension = Path.GetExtension(fileName); if (!string.IsNullOrEmpty(fileExtension)) { fileExtension = fileExtension.ToLowerInvariant(); } if (string.IsNullOrEmpty(contentType)) { contentType = PictureUtility.GetContentType(fileExtension); } var picture = new Media() { Binary = fileBinary, MimeType = contentType, Name = fileName, SystemName = Guid.NewGuid().ToString("n") }; _pictureService.WritePictureBytes(picture, _mediaSettings.PictureSaveLocation); _pictureService.AttachMediaToEntity(artistPage, picture); newImageUrl = _pictureService.GetPictureUrl(picture.Id); } return(Response(new { Success = true, Url = newImageUrl })); }
public IHttpActionResult UploadPicture(int BattleId, BattleType BattleType, IEnumerable <HttpPostedFileBase> file) { //first get battle var videoBattle = _videoBattleService.GetById(BattleId); var sponsors = _sponsorService.GetSponsors(_workContext.CurrentCustomer.Id, BattleId, BattleType, SponsorshipStatus.Accepted); //only the battle owner or the sponsor can upload the picture if (!(sponsors.Any() || videoBattle.ChallengerId == _workContext.CurrentCustomer.Id)) { return(Json(new { Success = false, Message = "Unauthorized" })); } var files = file.ToList(); var newImages = new List <object>(); foreach (var fi in files) { Stream stream = null; var fileName = ""; var contentType = ""; if (file == null) { throw new ArgumentException("No file uploaded"); } stream = fi.InputStream; fileName = Path.GetFileName(fi.FileName); contentType = fi.ContentType; var fileBinary = new byte[stream.Length]; stream.Read(fileBinary, 0, fileBinary.Length); var fileExtension = Path.GetExtension(fileName); if (!string.IsNullOrEmpty(fileExtension)) { fileExtension = fileExtension.ToLowerInvariant(); } if (string.IsNullOrEmpty(contentType)) { contentType = PictureUtility.GetContentType(fileExtension); } var picture = _pictureService.InsertPicture(fileBinary, contentType, null); newImages.Add(new { ImageUrl = _pictureService.GetPictureUrl(picture.Id), ImageId = picture.Id }); } return(Json(new { Success = true, Images = newImages })); }
public IHttpActionResult UploadPictures() { var files = HttpContext.Current.Request.Files; if (files.Count == 0) { VerboseReporter.ReportError("No file uploaded", "upload_pictures"); return(RespondFailure()); } var newImages = new List <object>(); for (var index = 0; index < files.Count; index++) { //the file var file = files[index]; //and it's name var fileName = file.FileName; //stream to read the bytes var stream = file.InputStream; var pictureBytes = new byte[stream.Length]; stream.Read(pictureBytes, 0, pictureBytes.Length); //file extension and it's type var fileExtension = Path.GetExtension(fileName); if (!string.IsNullOrEmpty(fileExtension)) { fileExtension = fileExtension.ToLowerInvariant(); } var contentType = file.ContentType; if (string.IsNullOrEmpty(contentType)) { contentType = PictureUtility.GetContentType(fileExtension); } var picture = new Media() { Binary = pictureBytes, MimeType = contentType, Name = fileName }; _mediaService.WritePictureBytes(picture, _mediaSettings.PictureSaveLocation); //save it _mediaService.Insert(picture); newImages.Add(new { ImageUrl = _mediaService.GetPictureUrl(picture.Id), ThumbnailUrl = _mediaService.GetPictureUrl(picture.Id, PictureSizeNames.ThumbnailImage), ImageId = picture.Id, MimeType = contentType }); } return(RespondSuccess(new { Images = newImages })); }
public IHttpActionResult UploadPictures() { var files = HttpContext.Current.Request.Files; if (files.Count == 0) { return(Response(new { Success = false, Message = "No file uploaded" })); } var newImages = new List <object>(); for (var index = 0; index < files.Count; index++) { //the file var file = files[index]; //and it's name var fileName = file.FileName; //stream to read the bytes var stream = file.InputStream; var pictureBytes = new byte[stream.Length]; stream.Read(pictureBytes, 0, pictureBytes.Length); //file extension and it's type var fileExtension = Path.GetExtension(fileName); if (!string.IsNullOrEmpty(fileExtension)) { fileExtension = fileExtension.ToLowerInvariant(); } var contentType = file.ContentType; if (string.IsNullOrEmpty(contentType)) { contentType = PictureUtility.GetContentType(fileExtension); } var picture = new Media() { Binary = pictureBytes, MimeType = contentType, Name = fileName }; _pictureService.WritePictureBytes(picture, _mediaSettings.PictureSaveLocation); newImages.Add(new { ImageUrl = _pictureService.GetPictureUrl(picture.Id), SmallImageUrl = _pictureService.GetPictureUrl(picture.Id), ImageId = picture.Id, MimeType = contentType }); } return(Json(new { Success = true, Images = newImages })); }
Song SaveRemoteSongToDB(string songJson) { if (string.IsNullOrEmpty(songJson)) { return(null); } var song = (JObject)JsonConvert.DeserializeObject(songJson); var songPage = new Song() { PageOwnerId = _workContext.CurrentCustomer.IsAdmin() ? _workContext.CurrentCustomer.Id : 0, Description = song["Description"].ToString(), Name = song["Name"].ToString(), RemoteEntityId = song["RemoteEntityId"].ToString(), RemoteSourceName = song["RemoteSourceName"].ToString(), PreviewUrl = song["PreviewUrl"].ToString(), TrackId = song["TrackId"].ToString(), RemoteArtistId = song["ArtistId"].ToString(), Published = true }; _songService.Insert(songPage); //we can now download the image from the server and store it on our own server //use the json we retrieved earlier if (!string.IsNullOrEmpty(song["ImageUrl"].ToString())) { var imageUrl = song["ImageUrl"].ToString(); var imageBytes = HttpHelper.ExecuteGET(imageUrl); if (imageBytes != null) { var fileExtension = Path.GetExtension(imageUrl); if (!String.IsNullOrEmpty(fileExtension)) { fileExtension = fileExtension.ToLowerInvariant(); } var contentType = PictureUtility.GetContentType(fileExtension); var picture = _pictureService.InsertPicture(imageBytes, contentType, songPage.GetSeName(_workContext.WorkingLanguage.Id, true, false)); var songPicture = new SongPicture() { EntityId = songPage.Id, DateCreated = DateTime.Now, DateUpdated = DateTime.Now, DisplayOrder = 1, PictureId = picture.Id }; _songService.InsertPicture(songPicture); } } return(songPage); }
ArtistPage SaveRemoteArtistToDB(string artistJson) { if (string.IsNullOrEmpty(artistJson)) { return(null); } var artist = (JObject)JsonConvert.DeserializeObject(artistJson); ArtistPage artistPage = new ArtistPage() { PageOwnerId = _workContext.CurrentCustomer.IsAdmin() ? _workContext.CurrentCustomer.Id : 0, Biography = artist["Description"].ToString(), Name = artist["Name"].ToString(), Gender = artist["Gender"].ToString(), HomeTown = artist["HomeTown"].ToString(), RemoteEntityId = artist["RemoteEntityId"].ToString(), RemoteSourceName = artist["RemoteSourceName"].ToString(), ShortDescription = "", }; _artistPageService.Insert(artistPage); //we can now download the image from the server and store it on our own server //use the json we retrieved earlier if (!string.IsNullOrEmpty(artist["ImageUrl"].ToString())) { var imageUrl = artist["ImageUrl"].ToString(); var imageBytes = HttpHelper.ExecuteGET(imageUrl); if (imageBytes != null) { var fileExtension = Path.GetExtension(imageUrl); if (!String.IsNullOrEmpty(fileExtension)) { fileExtension = fileExtension.ToLowerInvariant(); } var contentType = PictureUtility.GetContentType(fileExtension); var picture = _pictureService.InsertPicture(imageBytes, contentType, artistPage.GetSeName(_workContext.WorkingLanguage.Id, true, false)); var artistPicture = new ArtistPagePicture() { EntityId = artistPage.Id, DateCreated = DateTime.Now, DateUpdated = DateTime.Now, DisplayOrder = 1, PictureId = picture.Id }; _artistPageService.InsertPicture(artistPicture); } } return(artistPage); }
public Media GetMediaInstance(IFormFile mediaFile, int userId) { byte[] fileBytes; using (var stream = new MemoryStream()) { mediaFile.CopyTo(stream); fileBytes = stream.ToArray(); } var saveDirectory = ServerHelper.MapPath(ApplicationConfig.MediaDirectory, true); var fileName = SafeWriteBytesToFile(mediaFile.FileName, saveDirectory, fileBytes); //file extension and it's type var fileExtension = _localFileProvider.GetExtension(mediaFile.FileName); if (!string.IsNullOrEmpty(fileExtension)) { fileExtension = fileExtension.ToLowerInvariant(); } var contentType = mediaFile.ContentType; //an image? if (string.IsNullOrEmpty(contentType)) { contentType = PictureUtility.GetContentType(fileExtension); } //no? a video? if (string.IsNullOrEmpty(contentType)) { contentType = VideoUtility.GetContentType(fileExtension); } //no? let it be default one then if (string.IsNullOrEmpty(contentType)) { contentType = "application/octet-stream"; } var media = new Media() { MimeType = contentType, UserId = userId, Name = mediaFile.FileName, CreatedOn = DateTime.UtcNow, SystemName = fileName, AlternativeText = mediaFile.FileName }; media.ThumbnailPath = GetPictureUrl(media, 150, 150, true); media.LocalPath = GetPictureUrl(media, 0, 0, true); return(media); }
ArtistPage SaveRemoteArtistToDB(string artistJson) { if (string.IsNullOrEmpty(artistJson)) { return(null); } var artist = (JObject)JsonConvert.DeserializeObject(artistJson); var artistPage = new ArtistPage() { PageOwnerId = ApplicationContext.Current.CurrentUser.IsAdministrator() ? ApplicationContext.Current.CurrentUser.Id : 0, Biography = artist["Description"].ToString(), Name = artist["Name"].ToString(), Gender = artist["Gender"].ToString(), HomeTown = artist["HomeTown"].ToString(), RemoteEntityId = artist["RemoteEntityId"].ToString(), RemoteSourceName = artist["RemoteSourceName"].ToString(), ShortDescription = "", }; _artistPageService.Insert(artistPage); //we can now download the image from the server and store it on our own server //use the json we retrieved earlier if (!string.IsNullOrEmpty(artist["ImageUrl"].ToString())) { var imageUrl = artist["ImageUrl"].ToString(); var imageBytes = HttpHelper.ExecuteGet(imageUrl); if (imageBytes != null) { var fileExtension = Path.GetExtension(imageUrl); if (!String.IsNullOrEmpty(fileExtension)) { fileExtension = fileExtension.ToLowerInvariant(); } var contentType = PictureUtility.GetContentType(fileExtension); var picture = new Media() { Binary = imageBytes, MimeType = contentType, Name = artistPage.Name }; _pictureService.WritePictureBytes(picture, _mediaSettings.PictureSaveLocation); _pictureService.AttachMediaToEntity(artistPage, picture); } } return(artistPage); }
public IHttpActionResult UploadCover() { var files = HttpContext.Current.Request.Files; if (files.Count == 0) { return(Response(new { Success = false, Message = "No file uploaded" })); } //the file var file = files[0]; //and it's name var fileName = file.FileName; //stream to read the bytes var stream = file.InputStream; var pictureBytes = new byte[stream.Length]; stream.Read(pictureBytes, 0, pictureBytes.Length); //file extension and it's type var fileExtension = Path.GetExtension(fileName); if (!string.IsNullOrEmpty(fileExtension)) { fileExtension = fileExtension.ToLowerInvariant(); } var contentType = file.ContentType; if (string.IsNullOrEmpty(contentType)) { contentType = PictureUtility.GetContentType(fileExtension); } //save the picture now var picture = _pictureService.InsertPicture(pictureBytes, contentType, null); var image = new { ImageUrl = _pictureService.GetPictureUrl(picture.Id), SmallImageUrl = _pictureService.GetPictureUrl(picture.Id, _mobSocialSettings.TimelineSmallImageWidth), ImageId = picture.Id, MimeType = contentType }; return(Json(new { Success = true, Image = image })); }
public ActionResult UploadPicture(int SongId, IEnumerable <HttpPostedFileBase> file) { //first get song var song = _songService.GetById(SongId); if (!CanEdit(song)) { return(Json(new { Success = false, Message = "Unauthorized" })); } var files = file.ToList(); var imageUrl = ""; var product = _productService.GetProductById(song.AssociatedProductId); if (product == null) { return(Json(new { Success = false, Message = "NoAssociatedProduct" })); } foreach (var fi in files) { Stream stream = null; var fileName = ""; var contentType = ""; if (file == null) { throw new ArgumentException("No file uploaded"); } stream = fi.InputStream; fileName = Path.GetFileName(fi.FileName); contentType = fi.ContentType; var fileBinary = new byte[stream.Length]; stream.Read(fileBinary, 0, fileBinary.Length); var fileExtension = Path.GetExtension(fileName); if (!String.IsNullOrEmpty(fileExtension)) { fileExtension = fileExtension.ToLowerInvariant(); } if (String.IsNullOrEmpty(contentType)) { contentType = PictureUtility.GetContentType(fileExtension); } var picture = _pictureService.InsertPicture(fileBinary, contentType, null); var firstSongPicture = _songService.GetFirstEntityPicture(SongId); if (firstSongPicture == null) { firstSongPicture = new SongPicture() { EntityId = SongId, DateCreated = DateTime.Now, DateUpdated = DateTime.Now, DisplayOrder = 1, PictureId = picture.Id }; _songService.InsertPicture(firstSongPicture); } else { firstSongPicture.EntityId = SongId; firstSongPicture.DateCreated = DateTime.Now; firstSongPicture.DateUpdated = DateTime.Now; firstSongPicture.DisplayOrder = 1; firstSongPicture.PictureId = picture.Id; _songService.UpdatePicture(firstSongPicture); } //add the same picture to product as well product.ProductPictures.Add(new ProductPicture() { PictureId = firstSongPicture.PictureId, ProductId = product.Id }); imageUrl = _pictureService.GetPictureUrl(firstSongPicture.PictureId, 0, true); } return(Json(new { Success = true, Url = imageUrl })); }
public void UploadFile(int entityId, string entityName, IEnumerable <HttpPostedFileBase> file) { //if (!_permissionService.Authorize(StandardPermissionProvider.UploadPictures)) // return Json(new { success = false, error = "You do not have required permissions" }, "text/plain"); var files = file.ToList(); foreach (var fi in files) { //we process it distinct ways based on a browser //find more info here http://stackoverflow.com/questions/4884920/mvc3-valums-ajax-file-upload Stream stream = null; var fileName = ""; var contentType = ""; if (file == null) { throw new ArgumentException("No file uploaded"); } stream = fi.InputStream; fileName = Path.GetFileName(fi.FileName); contentType = fi.ContentType; var fileBinary = new byte[stream.Length]; stream.Read(fileBinary, 0, fileBinary.Length); var fileExtension = Path.GetExtension(fileName); if (!String.IsNullOrEmpty(fileExtension)) { fileExtension = fileExtension.ToLowerInvariant(); } if (String.IsNullOrEmpty(contentType)) { contentType = PictureUtility.GetContentType(fileExtension); } var picture = _pictureService.InsertPicture(fileBinary, contentType, null); var firstBusinessPagePicture = _businessPageService.GetFirstEntityPicture(entityId); if (firstBusinessPagePicture == null) { var businessPagePicture = new BusinessPagePicture() { EntityId = entityId, DateCreated = DateTime.Now, DateUpdated = DateTime.Now, DisplayOrder = 1, PictureId = picture.Id }; _businessPageService.InsertPicture(businessPagePicture); } else { firstBusinessPagePicture.EntityId = entityId; firstBusinessPagePicture.DateCreated = DateTime.Now; firstBusinessPagePicture.DateUpdated = DateTime.Now; firstBusinessPagePicture.DisplayOrder = 1; firstBusinessPagePicture.PictureId = picture.Id; _businessPageService.UpdatePicture(firstBusinessPagePicture); } } }
public IHttpActionResult UploadPictures() { var request = HttpContext.Current.Request; var files = request.Files; if (files.Count == 0) { return(Response(new { Success = false })); } //a little hack here.//if we pass model as paramter to this method we get -> This method or property is not supported after HttpRequest.GetBufferlessInputStream has been invoked PictureCropModel cropModel = null; if (request.Params["crop"] != null) { int left, width, top, height; int.TryParse(request.Params["left"] ?? "0", out left); int.TryParse(request.Params["width"] ?? "0", out width); int.TryParse(request.Params["top"] ?? "0", out top); int.TryParse(request.Params["height"] ?? "0", out height); cropModel = new PictureCropModel() { Width = width, Height = height, Left = left, Top = top }; } var newImages = new List <object>(); for (var index = 0; index < files.Count; index++) { //the file var file = files[index]; //and it's name var fileName = file.FileName; //stream to read the bytes var stream = file.InputStream; var pictureBytes = new byte[stream.Length]; stream.Read(pictureBytes, 0, pictureBytes.Length); //file extension and it's type var fileExtension = Path.GetExtension(fileName); if (!string.IsNullOrEmpty(fileExtension)) { fileExtension = fileExtension.ToLowerInvariant(); } var contentType = file.ContentType; if (string.IsNullOrEmpty(contentType)) { contentType = PictureUtility.GetContentType(fileExtension); } var picture = new Media() { Binary = pictureBytes, MimeType = contentType, Name = fileName, UserId = _workContext.CurrentCustomer.Id, DateCreated = DateTime.UtcNow }; //should we crop or not if (cropModel?.Height > 0 && cropModel.Width > 0) { _mediaService.WritePictureBytesCropped(picture, cropModel.Left, cropModel.Top, cropModel.Width, cropModel.Height); } else { _mediaService.WritePictureBytes(picture); } //save it _mediaService.Insert(picture); newImages.Add(picture.ToModel(_mediaService, _mediaSettings, _workContext, _storeContext, _userService, _customerProfileService, _customerProfileViewService, _customerFollowService, _friendService, _commentService, _likeService, _pictureService, Url, _webHelper)); } return(Response(new { Success = true, Images = newImages })); }
public IHttpActionResult SaveArtist(ArtistPageModel model) { if (!ModelState.IsValid) { VerboseReporter.ReportError("Invalid data submitted. Please check all fields and try again.", "save_artist"); return(RespondFailure()); } if (!ApplicationContext.Current.CurrentUser.IsRegistered()) { VerboseReporter.ReportError("Unauthorized access", "save_artist"); return(RespondFailure()); } //check to see if artist name already exists string artistJson; if (IsArtistPageNameAvailable(model.Name, out artistJson)) { var artistPage = new ArtistPage() { PageOwnerId = ApplicationContext.Current.CurrentUser.Id, Biography = model.Description, Name = model.Name, DateOfBirth = model.DateOfBirth, Gender = model.Gender, HomeTown = model.HomeTown, RemoteEntityId = model.RemoteEntityId, RemoteSourceName = model.RemoteSourceName, ShortDescription = model.ShortDescription }; _artistPageService.Insert(artistPage); if (artistJson != "") { //we can now download the image from the server and store it on our own server //use the json we retrieved earlier var jObject = (JObject)JsonConvert.DeserializeObject(artistJson); if (!string.IsNullOrEmpty(jObject["ImageUrl"].ToString())) { var imageUrl = jObject["ImageUrl"].ToString(); var imageBytes = HttpHelper.ExecuteGet(imageUrl); var fileExtension = Path.GetExtension(imageUrl); if (!string.IsNullOrEmpty(fileExtension)) { fileExtension = fileExtension.ToLowerInvariant(); } var contentType = PictureUtility.GetContentType(fileExtension); var picture = new Media() { Binary = imageBytes, Name = model.Name, MimeType = contentType }; _pictureService.WritePictureBytes(picture, _mediaSettings.PictureSaveLocation); //relate both page and picture _pictureService.AttachMediaToEntity(artistPage, picture); } } return(Response(new { Success = true, RedirectTo = Url.Route("ArtistPageUrl", new RouteValueDictionary() { { "SeName", artistPage.GetPermalink() } }) })); } else { return(Response(new { Success = false, Message = "DuplicateName" })); } }
public IActionResult UploadPictures() { var httpPostedFiles = Request.Form.Files; if (httpPostedFiles.Count == 0) { VerboseReporter.ReportError("No file uploaded", "upload_pictures"); return(RespondFailure()); } var newImages = new List <object>(); for (var index = 0; index < httpPostedFiles.Count; index++) { //the file var file = httpPostedFiles[index]; //and it's name //var fileName = file.FileName; var httpPostedFile = httpPostedFiles.FirstOrDefault(); if (httpPostedFile == null) { return(Json(new { success = false, message = "No file uploaded", downloadGuid = Guid.Empty })); } var fileBinary = _downloadService.GetDownloadBits(httpPostedFile); const string qqFileNameParameter = "qqfilename"; var fileName = httpPostedFile.FileName; if (string.IsNullOrEmpty(fileName) && Request.Form.ContainsKey(qqFileNameParameter)) { fileName = Request.Form[qqFileNameParameter].ToString(); } //remove path (passed in IE) fileName = _fileProvider.GetFileName(fileName); var contentType = httpPostedFile.ContentType; var fileExtension = _fileProvider.GetFileExtension(fileName); if (!string.IsNullOrEmpty(fileExtension)) { fileExtension = fileExtension.ToLowerInvariant(); } //stream to read the bytes //var stream = file.InputStream; //var pictureBytes = new byte[stream.Length]; //stream.Read(pictureBytes, 0, pictureBytes.Length); if (string.IsNullOrEmpty(contentType)) { contentType = PictureUtility.GetContentType(fileExtension); } var media = new Media() { Binary = fileBinary, MimeType = contentType, Name = fileName, UserId = CurrentUser.Id, DateCreated = DateTime.UtcNow, MediaType = MediaType.Image }; _mediaService.WritePictureBytes(media, _generalSettings, _mediaSettings); //save it _mediaService.Insert(media); //newImages.Add(picture.ToModel(_mediaService, _mediaSettings)); newImages.Add(media); } return(RespondSuccess(new { Images = newImages })); }