使用C#MVC将csv(保存在解决方案中)发送到前端

使用C#MVC将csv(保存在解决方案中)发送到前端,c#,csv,model-view-controller,download,C#,Csv,Model View Controller,Download,我是C#新手,我正在使用MVC构建一个带有前端的web刮板。应用程序上载一个ID文件,使用selenium查找上载的ID,收集数据,然后将其写入CSV。然后,CSV以文件路径“/files/output.CSV”保存在解决方案中 该功能在将output.csv写入并保存到解决方案的过程中一直有效但我不知道如何将该CSV发送回前端的用户。在写入“/files/output.CSV”后,控制器点击一行,显示 return Ok(new { count = files.Count, size, fil

我是C#新手,我正在使用MVC构建一个带有前端的web刮板。应用程序上载一个ID文件,使用selenium查找上载的ID,收集数据,然后将其写入CSV。然后,CSV以文件路径“/files/output.CSV”保存在解决方案中

该功能在将output.csv写入并保存到解决方案的过程中一直有效但我不知道如何将该CSV发送回前端的用户。在写入“/files/output.CSV”后,控制器点击一行,显示

return Ok(new { count = files.Count, size, filePath });
屏幕上会出现一行简单的JSON,给出上传文件的计数/大小/文件路径

这是我的控制器

public class ScraperController : Controller
{
    private readonly IScraperService _scraperService;

    public ScraperController(IScraperService scraperService)
    {
        _scraperService = scraperService;
    }

    [HttpPost("Scraper")]
    public async Task<IActionResult> Post(List<IFormFile> files)
    {
        long size = files.Sum(f => f.Length);

        var filePath = Path.GetTempFileName();

        foreach (var formFile in files)
        {
            if (formFile.Length > 0)
            {
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await formFile.CopyToAsync(stream);
                }
                List<string> memberIds = _scraperService.Read(filePath);
                List<Result> results = _scraperService.Scrape(memberIds);
                _scraperService.Write(results);
            }
        }

        return Ok(new { count = files.Count, size, filePath });
    }

    public IActionResult Index()
    {
        return View();
    }
公共类ScraperController:控制器
{
私有只读IScraperService(u scraperService);;
公共刮板控制器(IScraperService刮板服务)
{
_scraperService=scraperService;
}
[HttpPost(“刮板”)]
公共异步任务发布(列表文件)
{
long size=files.Sum(f=>f.Length);
var filePath=Path.GetTempFileName();
foreach(文件中的var formFile)
{
如果(formFile.Length>0)
{
使用(var stream=newfilestream(filePath,FileMode.Create))
{
等待formFile.CopyToAsync(流);
}
List memberIds=_scraperService.Read(filePath);
列表结果=_scraperService.Scrape(memberid);
_scraperService.Write(结果);
}
}
返回Ok(新的{count=files.count,size,filePath});
}
公共IActionResult索引()
{
返回视图();
}
如何将新编写的.csv发送回用户?是否需要其他ActionResult?是否需要其他控制器?是否需要其他视图?

只需返回一个文件:

  return File(new System.Text.UTF8Encoding().GetBytes(yourFile), "text/csv", "yourFileName.csv");

你想在前端显示你的数据吗?你能像这样返回一个文件吗?返回文件(new System.Text.UTF8Encoding().GetBytes(yourFile),“Text/csv”,“your name.csv”);是的,我只想返回用户可以下载(或自动下载)的文件。我不需要显示数据。