首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Mediatr模式从download中的wwwroot文件夹下载文件?

如何使用Mediatr模式从download中的wwwroot文件夹下载文件?
EN

Stack Overflow用户
提问于 2022-06-20 10:03:10
回答 1查看 317关注 0票数 1

我试图下载www.root文件,其中文件保存,我想下载文件的文件,只有文件名,这是我从我的角码发送。在控制器中,我的代码如下

代码语言:javascript
复制
[HttpPost("DownloadMPWorthyFile")]
        public async Task<ActionResult<bool>> DownloadMPWorthyFile(DownloadMPWorthyFileCommand command)
        {
            return await Mediator.Send(command);
        }

在DownloadMPWorthyFileCommand命令中,我添加了

代码语言:javascript
复制
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;
        }
    }

}

我用这种方式编写了代码,但它不能作为返回类型,我一直在出错,有人知道从wwwroot下载文件的正确方法吗?

EN

回答 1

Stack Overflow用户

发布于 2022-06-26 19:04:12

您应该检查IHostingEvironment,有一个WebRootFileProvider,您可以用它检查文件并打开这些文件的流。

代码语言:javascript
复制
var file = _environment.WebRootFileProvider.GetFileInfo("...");
if (file.Exists)
{
    var stream = file.CreateReadStream();
    // Go on from here...
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72685330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档