[英]C# MVC - Passing a method to my HomeController index and using it in my front end
[英]Send a csv (saved in the solution) to the front end using C# MVC
我是C#的新手,並且正在使用MVC構建帶有前端的Web刮板。 該應用程序上載ID文件,使用硒查找上載的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();
}
如何將新編寫的.csv發送回用戶? 我是否需要另一個ActionResult? 我需要另一個控制器嗎? 我需要其他視圖嗎?
只需返回一個文件:
return File(new System.Text.UTF8Encoding().GetBytes(yourFile), "text/csv", "yourFileName.csv");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.