我目前正在尝试创建一个按钮,可以将文本文件从我的网站下载到用户计算机。我有正确的文件上传到网站,但下载它的代码不起作用。这就是我所拥有的:
private ActionResult CallProgram(string path) //calls program that outputs txt file to website
{
using (Process exeProcess = Process.Start(startInfo)) //program called
{
lines = exeProcess.StandardOutput.ReadToEnd();
Information info = new Information();
//save txt to website
info.fileName = Path.GetFileName(path);
info.filePath = Path.Combine(Server.MapPath("~/App_Data/uploads"), info.fileName + "_results.txt");
using (StreamWriter outfile = new StreamWriter(info.filePath))
{
outfile.Write(lines);
outfile.Close();
} //text output saved to txt
}
return RedirectToAction("results_download", new { output = info });
}
以下是行动:
public ActionResult results_download(Information output)
{
return View(output);
}
aspx文件中按钮的实际代码:
<script runat="server">
protected System.IO.File download_BTN_Click(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment; filename=Model.fileName");
Response.TransmitFile(Server.MapPath("~/App_Data/uploads/"+info.fileName)); //
Response.End();
}
<div>
<asp:Button ID="download_BTN" runat="server" OnClick="download_BTN_Click" fileName="" Text="Download Results" />
</div>
我是asp.net的新手,并且给了一个远远超出我能力的项目。所以我可能只是犯了一个愚蠢的错误。谢谢。