Friday, 30 August 2019

File download in CORE(2.1) & MVC6

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class JagannathController : Controller
    {
        private readonly IRepository<JBS> JRepository;
        private readonly IHostingEnvironment hostingEnvironment;
        public JagannathController(IRepository<JBS> jRepository, IHostingEnvironment HostingEnvironment)
        {
            this.JRepository = jRepository;
            this.hostingEnvironment = HostingEnvironment;
        }
       
        [HttpGet]
        public IActionResult down()
        {
            string Filename = "173f561f-a204-425a-a77d-7ca0acdd0c79_HPSM.png";
            string downloadFolder = string.Empty;
            downloadFolder = Path.Combine(hostingEnvironment.WebRootPath, @"images\");
            byte[] fileBytes = System.IO.File.ReadAllBytes(downloadFolder + Filename);
            return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, Filename);
        }
    }
}

No comments:

Post a Comment