我试图下载www.root文件,其中文件保存,我想下载文件的文件,只有文件名,这是我从我的角码发送。在控制器中,我的代码如下
[HttpPost("DownloadMPWorthyFile")]
public async Task<ActionResult<bool>> DownloadMPWorthyFile(DownloadMPWorthyFileCommand command)
{
return await Mediator.Send(command);
}在DownloadMPWorthyFileCommand命令中,我添加了
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下载文件的正确方法吗?
发布于 2022-06-26 19:04:12
您应该检查IHostingEvironment,有一个WebRootFileProvider,您可以用它检查文件并打开这些文件的流。
var file = _environment.WebRootFileProvider.GetFileInfo("...");
if (file.Exists)
{
var stream = file.CreateReadStream();
// Go on from here...
}https://stackoverflow.com/questions/72685330
复制相似问题