public bool ProcessMediaItemSubmission(HttpContext context, ref MediaItem mediaItem, int userId) { try { var sr = new System.IO.StreamReader(context.Request.InputStream); string jsonContent = sr.ReadToEnd(); var submission = JsonConvert.DeserializeObject <Common.Model.Submissions.MediaItemSubmission>(jsonContent); if (submission.ImageDataBase64 == null) { return(false); } string filePrefix = DateTime.UtcNow.Millisecond.ToString() + "_"; var tempFiles = new List <string>(); string tempFolder = context.Server.MapPath("~/temp/uploads/"); string tmpFileName = tempFolder + filePrefix + submission.ChargePointID; if (submission.ImageDataBase64.StartsWith("data:image/jpeg")) { tmpFileName += ".jpg"; } if (submission.ImageDataBase64.StartsWith("data:image/png")) { tmpFileName += ".png"; } if (submission.ImageDataBase64.StartsWith("data:image/tiff")) { tmpFileName += ".tiff"; } var image = Base64ToImage(submission.ImageDataBase64); image.Save(tmpFileName); if (submission.ImageDataBase64.StartsWith("data:")) { submission.ImageDataBase64 = submission.ImageDataBase64.Substring(submission.ImageDataBase64.IndexOf(',') + 1, submission.ImageDataBase64.Length - (submission.ImageDataBase64.IndexOf(',') + 1)); } File.WriteAllBytes(tmpFileName, Convert.FromBase64String(submission.ImageDataBase64)); tempFiles.Add(tmpFileName); var task = Task.Factory.StartNew(() => { var mediaManager = new MediaItemManager(); foreach (var tmpFile in tempFiles) { var photoAdded = mediaManager.AddPOIMediaItem(tempFolder, tmpFile, submission.ChargePointID, submission.Comment, false, userId); } }, TaskCreationOptions.LongRunning); return(true); } catch { return(false); } }
public async Task <bool> ProcessMediaItemSubmission(string uploadPath, HttpContext context, MediaItem mediaItem, int userId) { if (context.Request.Form.Files.Count == 0) { return(false); } try { var files = context.Request.Form.Files; string filePrefix = DateTime.UtcNow.Millisecond.ToString() + "_"; int chargePointId = int.Parse(context.Request.Form["id"]); string comment = context.Request.Form["comment"]; var tempFiles = new List <string>(); foreach (var postedFile in context.Request.Form.Files) { if (postedFile != null && postedFile.Length > 0) { string tmpFile = uploadPath + "\\" + filePrefix + postedFile.FileName; using (var stream = new FileStream(tmpFile, FileMode.Create)) { await postedFile.CopyToAsync(stream); } tempFiles.Add(tmpFile); } } var task = Task.Factory.StartNew(() => { var mediaManager = new MediaItemManager(); foreach (var tmpFile in tempFiles) { var photoAdded = mediaManager.AddPOIMediaItem(uploadPath, tmpFile, chargePointId, comment, false, userId); } }, TaskCreationOptions.LongRunning); return(true); } catch { return(false); } }
public bool ProcessMediaItemSubmission(HttpContext context, ref MediaItem mediaItem, int userId) { try { var files = context.Request.Files; string filePrefix = DateTime.UtcNow.Millisecond.ToString() + "_"; int chargePointId = int.Parse(context.Request["id"]); string comment = context.Request["comment"]; var tempFiles = new List <string>(); string tempFolder = context.Server.MapPath("~/temp/uploads/"); foreach (string file in context.Request.Files) { var postedFile = context.Request.Files[file]; if (postedFile != null && postedFile.ContentLength > 0) { string tmpFile = tempFolder + filePrefix + postedFile.FileName; postedFile.SaveAs(tmpFile); tempFiles.Add(tmpFile); } } var task = Task.Factory.StartNew(() => { var mediaManager = new MediaItemManager(); foreach (var tmpFile in tempFiles) { var photoAdded = mediaManager.AddPOIMediaItem(tempFolder, tmpFile, chargePointId, comment, false, userId); } }, TaskCreationOptions.LongRunning); return(true); } catch { return(false); } }