【发布时间】:2017-07-11 10:10:06
【问题描述】:
我正在寻求帮助。我有一个没有 View 的 MVC 动作,这个动作的功能是下载一个 CSV 文件。我想使用控制台应用程序来调用操作以下载文件。
我的控制器:
Public void DownloadCSV()
{
StringWriter sw = new StringWriter();
sw.WriteLine("1,2,3,4");
string remark = "Cool"; // -- Edited
Response.AddHeader("Content-Disposition", "attachment; filename=Download-"+remark+".csv"); // -- edited
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.Write(sw);
Response.End();
}
我的控制台应用程序:
class Program
{
static void Main(string[] args)
{
using (var client = new WebClient())
{
string result = client.DownloadString("http://localhost:2114/File/DownloadCSV");
}
}
}
此 Web 应用程序需要登录。这就是我无法访问此网址的原因吗?
【问题讨论】:
-
运行控制台应用程序时会发生什么?
-
@mjwills 我试图运行它但没有任何反应
-
我无法找到您从控制台应用程序调用 DownloadCSV() 方法的位置。你有没有检查过它的调用与否?
-
定义
nothing happens。你的意思是result是一个空白字符串吗?还有什么? -
如果您使用 Postman,并针对 localhost:2114/File/DownloadCSV 执行 GET,它会显示什么结果? chrome.google.com/webstore/detail/postman/…
标签: c# asp.net-mvc csv console-application