Here, I will explain how to upload and download files in ASP.NET Core 2.0.
First of all create an empty project with ASP.NET Core 2.0 and Update the Startup.cs file to add services and middle ware for MVC.
Include this NameSpaces :
Create a model class in Model/Demo folder.
1. IFormFileExtensions.cs :
2. FilesViewModel.cs :
1. Index.cshtml :
2. Files.cshtml :
Now, You can able to upload file and download file.
First of all create an empty project with ASP.NET Core 2.0 and Update the Startup.cs file to add services and middle ware for MVC.
public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddSingletonNow, Add a Controller with name "DemoController" and copy paste below action methods to upload and download the files.( new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"))); services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseDeveloperExceptionPage(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Demo}/{action=Index}/{id?}"); }); } }
Include this NameSpaces :
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; using System.Collections.Generic; using System.Threading.Tasks; using System.IO; using Microsoft.Extensions.FileProviders; using Nikhil.Mvc.FileUpload.Models.Home; // call model folder here public class DemoController : Controller { private readonly IFileProvider fileProvider; public DemoController(IFileProvider fileProvider) { this.fileProvider = fileProvider; } public IActionResult Index() { return View(); } [HttpPost] public async TaskUploadFiles(List files) { if (files == null || files.Count == 0) return Content("files not selected"); foreach (var file in files) { var path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot", file.GetFilename()); using (var stream = new FileStream(path, FileMode.Create)) { await file.CopyToAsync(stream); } } return RedirectToAction("Files"); } public async Task Download(string filename) { if (filename == null) return Content("filename not present"); var path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot", filename); var memory = new MemoryStream(); using (var stream = new FileStream(path, FileMode.Open)) { await stream.CopyToAsync(memory); } memory.Position = 0; return File(memory, GetContentType(path), Path.GetFileName(path)); } private string GetContentType(string path) { var types = GetMimeTypes(); var ext = Path.GetExtension(path).ToLowerInvariant(); return types[ext]; } private Dictionary GetMimeTypes() { return new Dictionary { {".txt", "text/plain"}, {".pdf", "application/pdf"}, {".doc", "application/vnd.ms-word"}, {".docx", "application/vnd.ms-word"}, {".xls", "application/vnd.ms-excel"}, {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, {".png", "image/png"}, {".jpg", "image/jpeg"}, {".jpeg", "image/jpeg"}, {".gif", "image/gif"}, {".csv", "text/csv"} }; } }
Create a model class in Model/Demo folder.
1. IFormFileExtensions.cs :
public static class IFormFileExtensions { public static string GetFilename(this IFormFile file) { return ContentDispositionHeaderValue.Parse( file.ContentDisposition).FileName.ToString().Trim('"'); } public static async TaskGetFileStream(this IFormFile file) { MemoryStream filestream = new MemoryStream(); await file.CopyToAsync(filestream); return filestream; } public static async Task GetFileArray(this IFormFile file) { MemoryStream filestream = new MemoryStream(); await file.CopyToAsync(filestream); return filestream.ToArray(); } }
2. FilesViewModel.cs :
public class FileDetails { public string Name { get; set; } public string Path { get; set; } } public class FilesViewModel { public ListNow, Add a Razor page with HTML form to upload a file.Files { get; set; } = new List (); }
1. Index.cshtml :
2. Files.cshtml :
Now, You can able to upload file and download file.
Comments
Post a Comment