如何 return 导出路径而不使用复制到 memory2?

How to return export path without using copy to memory2?

我在网络上工作API asp.net core 3.1 我需要return直接导出路径而不复制到内存

请问怎么做?

如何return导出路径而不复制到内存,如下所示

return File(memory2, "text/plain", Path.GetFileName(exportPath));

表示使用导出路径而不是使用内存2

在文件导出路径上得到结​​果后我需要另一个意思

return直接不用copy到内存

我尝试过的:

public IActionResult Upload()
    {
foreach (var m in mods)
                      {
                          List<InputExcel> inputmodulelist = new List<InputExcel>();
                          inputmodulelist = inputexcellist.Where(x => x.ModuleName == m).ToList();
                          var dtimport = DatatableConversion.ToDataTable(inputmodulelist);
                          DataTable dtexport = new DataTable();
                          dtexport = _deliveryService.LoadExcelToDataTable(_connectionString, dtimport);
                          ex.Export(dtexport, m, exportPath);
            
                      }
                  }
                  var memory2 = new MemoryStream();
                  using (var stream = new FileStream(exportPath, FileMode.Open))
                  {
                      stream.CopyTo(memory2);
                  }
                  memory2.Position = 0;
            
                  return File(memory2, "text/plain", Path.GetFileName(exportPath));

从你的描述来看,你是想下载文件而不是将文件内容读入内存,对吧?

如果是这样,可以直接通过System.IO.File.ReadAllBytes()方法读取文件,代码如下:

[Route("api/[controller]")]
[ApiController]
public class ToDoController : ControllerBase
{
    private readonly ApplicationDbContext _dbContext;
    private readonly IWebHostEnvironment environment;
    public ToDoController(ApplicationDbContext context, IWebHostEnvironment hostEnvironment)
    {
        _dbContext = context;
        environment = hostEnvironment;
    } 
    [HttpGet("download")]
    public IActionResult Download()
    {
        var exportPath = Path.Combine( environment.WebRootPath, "files","Image1.jpg");
        return File(System.IO.File.ReadAllBytes(exportPath), "image/png", System.IO.Path.GetFileName(exportPath));
    }

调用上述action方法时,会下载图片文件。您可以更改 file's MIME type 以下载不同的文件。