public Task SendModelConfirmationAsync(ApplicationUser user, UploadModelConfirmation model) { var engine = new RazorLightEngineBuilder() .UseMemoryCachingProvider() .Build(); string template = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "wwwroot", "Templates", "Emails", "Model", "ModelConfirmation.cshtml")); string result = engine.CompileRenderAsync("Confirmation", template, model).Result; List <Attachment> ATT = new List <Attachment>(); foreach (var imglnk in model.ImageUrls) { string base64String = string.Empty; // Convert Image to Base64 using (FileStream image = new FileStream(Path.Combine("wwwroot", "images", "ObjectImages", imglnk), FileMode.Open)) { MemoryStream ms = new MemoryStream(); image.CopyTo(ms); Attachment a = new Attachment(); a.Content = Convert.ToBase64String(ms.ToArray()); a.ContentId = imglnk; a.Filename = imglnk; a.Type = "image/jpeg"; ATT.Add(a); } } return(Execute(Options.SendGridKey, "Congrats On Your Upload!", result, user.Email, ATT)); }
public async Task <JsonResult> UploadModelsAsync() { var files = HttpContext.Request.Form.Files; long size = files.Sum(f => f.Length); // full path to file in temp location Dictionary <string, string> TempFiles = new Dictionary <string, string>(); foreach (var formFile in files) { var filePath = Path.GetTempFileName(); TempFiles.Add(filePath, formFile.FileName); if (formFile.Length > 0) { using (var stream = new FileStream(filePath, FileMode.OpenOrCreate)) { await formFile.CopyToAsync(stream); } } } // process uploaded files List <string> FailedFiles = new List <string>(); //List of all added objects List <string> AddedModelPics = new List <string>(); foreach (var file in TempFiles.Keys) { //Remove the filepath string FileNameWithExt = TempFiles[file]; //remove the file extension string FileName = FileNameWithExt.Remove(FileNameWithExt.Length - 4); using (var stream = new FileStream(file, FileMode.Open)) { MemoryStream ValidateStream = new MemoryStream(); stream.CopyTo(ValidateStream); stream.Position = 0; // Reset the position for reading switch (FindFileType(ValidateStream, FileNameWithExt)) { case FileUploadType.STL: { if (!Add3DModelToFile(stream, FileName)) { FailedFiles.Add(FileName); } break; } case FileUploadType.PNG: case FileUploadType.JPG: { string pic = AddPictureRepresentation(stream, FileName) + ".png"; if (!String.IsNullOrEmpty(pic)) { AddedModelPics.Add(pic); } else { FailedFiles.Add(FileName); } break; } default: { FailedFiles.Add(FileName); continue; } } } } UploadModelJsonResponse response = new UploadModelJsonResponse() { failed_files = new List <string>() }; //Gather the confirmed file picture form the database, send that in an email if (AddedModelPics.Count > 0) { response.success = true; ApplicationUser user = await _UserManager.GetUserAsync(User); UploadModelConfirmation emailModel = new UploadModelConfirmation() { FirstName = user.FirstName, ImageUrls = new List <string>() }; foreach (var pic_file_loc in AddedModelPics) { //Load the picture as an absolute url emailModel.ImageUrls.Add(Url.Content(pic_file_loc)); } await _EmailSender.SendModelConfirmationAsync(user, emailModel); } //If there are any error files, Review them and send response. if (FailedFiles.Count > 0) { foreach (var error_file_name in FailedFiles) { //Add to reuslt Json Object error list response.failed_files.Add(error_file_name); } } return(Json(response)); }