Last active
May 28, 2019 08:21
-
-
Save AvgustPol/d92e20c94c4dc15a6f01550eeffb7aa9 to your computer and use it in GitHub Desktop.
Image Saver ASP.NET CORE + JS image preview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| responsive image style | |
| <style> | |
| .imageBox { | |
| max-width: 100%; | |
| height: auto; | |
| } | |
| </style> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @*Download form: *@ | |
| <div class="row"> | |
| <div class="col-md-12"> | |
| <form method="post" enctype="multipart/form-data"> | |
| <div class="form-group"> | |
| <div class="col-md-10"> | |
| <p>Upload one or more files using this form:</p> | |
| <input type="file" name="files" multiple /> | |
| </div> | |
| </div> | |
| <div class="form-group"> | |
| <div class="col-md-10"> | |
| <input type="submit" value="Upload" /> | |
| </div> | |
| </div> | |
| </form> | |
| </div> | |
| </div> | |
| @*Image preview: *@ | |
| <div class="row"> | |
| <div class="col-5"> | |
| <img class="imageBox" id="blah" src="#" alt="Preview of your image will be here" /> | |
| </div> | |
| </div> | |
| //optional JS loading | |
| //uncomment this if you want to download jquery library (just in case you didn't load it locally) | |
| @*<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>*@ | |
| <script> | |
| function readURL(input) { | |
| if (input.files && input.files[0]) { | |
| var reader = new FileReader(); | |
| reader.onload = function (e) { | |
| $('#blah').attr('src', e.target.result); | |
| } | |
| reader.readAsDataURL(input.files[0]); | |
| } | |
| } | |
| $("#imgInp").change(function () { | |
| readURL(this); | |
| }); | |
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Class: | |
| Used "usings" | |
| //using Microsoft.AspNetCore.Hosting; | |
| //using Microsoft.AspNetCore.Http; | |
| //using Microsoft.AspNetCore.Mvc.RazorPages; | |
| //using System.Collections.Generic; | |
| //using System.IO; | |
| Used "usings" | |
| //dont forget to add namespace ;) | |
| public class ImageSaverModel : PageModel | |
| { | |
| private readonly IHostingEnvironment _hostingEnvironment; | |
| public ImageSaverModel(IHostingEnvironment hostingEnvironment) | |
| { | |
| _hostingEnvironment = hostingEnvironment; | |
| } | |
| public void OnGet() | |
| { | |
| } | |
| public void OnPost(List<IFormFile> files) | |
| { | |
| foreach (var img in files) | |
| { | |
| if (img != null) | |
| { | |
| //Firstly we need to find the wwwroot path. | |
| //To find that we need a Hosting Environment object (to ask "him" about "what is the path of my wwwroot?") | |
| //We are using DI (dependency injection) and passing hostingEnvironment via controller | |
| string wwwrootPath = _hostingEnvironment.WebRootPath; | |
| string originalName = Path.GetFileName(img.FileName); | |
| string folderForSavedImages = "SavedImages"; | |
| var destinationPath = Path.Combine(wwwrootPath, folderForSavedImages, originalName); | |
| img.CopyTo(new FileStream(destinationPath, FileMode.Create)); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment