How to download file from wwwroot folder in dot core api using Mediatr pattern?

Viewed 127

I am trying to download wwwroot file where file is saved i want to download file by filename only which i am sending from my angular code. in controller my code is like this

[HttpPost("DownloadMPWorthyFile")]
        public async Task<ActionResult<bool>> DownloadMPWorthyFile(DownloadMPWorthyFileCommand command)
        {
            return await Mediator.Send(command);
        }

in DownloadMPWorthyFileCommand command i have added

using AutoMapper;
using Kaizen.Common.Interfaces;
using Kaizen.Common.Logger;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Kaizen.Domain.Entities;
using Kaizen.Application.Common.Services.EmailService;
using EmailActions = Kaizen.Domain.Enums.EmailActions.Actions;
using Kaizen.Common.Models;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Hosting;

namespace Kaizen.Application.Kaizen.Command
{
    public class DownloadMPWorthyFileCommand : IRequest<bool>
    {
        public string FileName { get; set; }
        public string PageName { get; set; }
        public string IdeaNumber { get; set; }
    }

    public class GetKaizenDownloadfileResponse
    {
        public int StatusCode { get; set; }
        public string Message { get; set; }
        public string ErrorMessage { get; set; }
        public List<getdata> Data { get; set; }
    }

    public class getdata
    {
        public string FileName { get; set; }
        public string Method { get; set; }
        public string PageName { get; set; }
    }
    public class DownloadMPWorthyFileCommandHandle : IRequestHandler<DownloadMPWorthyFileCommand, bool>
    {
        private readonly IKaizenDBContext _context;
        private readonly IMapper _mapper;
        private readonly ILogger<kaizenUploadFile> _logger;
        private readonly IEmailService _emailService;
        private IHostingEnvironment Environment;
        public DownloadMPWorthyFileCommandHandle(IKaizenDBContext context, IMapper mapper, ILogger<SendMailForMPWorthyCommandHandler> logger, IEmailService emailService, IOptions<AppSettings> appSettings, IHostingEnvironment _environment)
        {
            _context = context;
            _mapper = mapper;
            //_logger = logger;
            _emailService = emailService;
            Environment = _environment;

           // _appSettings = appSettings.Value;
        }

        public async Task<bool> Handle(DownloadMPWorthyFileCommand request, CancellationToken cancellationToken)
        {
            bool retval = false;
            var MPWorthyFile = _context.DocumentUploaded.Where(e => e.IdeaNumber == request.IdeaNumber && e.PageName==request.PageName);
            if (MPWorthyFile != null)
            {
                string wwwPath1 = this.Environment.WebRootPath;
                string imgnm1 = "/document/" + request.FileName;
                string filePath1 = wwwPath1 + imgnm1;
                string imageName1 = filePath1;

                var net1 = new System.Net.WebClient();
                var path1 = wwwPath1 + filePath1;
                var data1 = net1.DownloadData(filePath1);
                var content1 = new System.IO.MemoryStream(data1);
                retval= true;
            }
            else
            {
                retval = false;
            }
            return retval;
        }
    }

}

i have done code in this way but its not working in return type i keep getting error,is anyone know correct way to download the file from wwwroot?

1 Answers

You should check the IHostingEvironment, there is an WebRootFileProvider with which you check for file and open stream on those.

var file = _environment.WebRootFileProvider.GetFileInfo("...");
if (file.Exists)
{
    var stream = file.CreateReadStream();
    // Go on from here...
}
Related