我想在控制器的 GetFile 方法中生成一个文件,并将其作为指向我的视图的链接返回。用户应该能够单击链接并下载文件。我怎样才能在我的 Jquery 函数(数据)部分写这个。
@model PopulateDropDownList.Models.Populate
@using PopulateDropDownList.Models
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<script src="../../Scripts/jquery-1.4.4.js" type="text/javascript"></script>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new {rnctype = "multipart/form-data" }))
{
<p>
@Html.DropDownList("mylist", Helper.GetDescription(),"--select here--")
<input id="Button1" type="button" value="button" />
</p>
}
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$("#Button1").click(function () {
var SelCat = $("#mylist").val();
if (SelCat != 0) {
var url = '@Url.Action("GetFile", "Home")';
$.post(url, { categoryId: SelCat },
function (data) {
});
}
else {
alert("You need to select an city");
}
});
});
</script>
public FileStreamResult GetFile()
{
string name = "me.txt";
FileInfo info = new FileInfo(name);
if (!info.Exists)
{
using (StreamWriter writer = info.CreateText())
{
writer.WriteLine("Hello, I am a new text file");
}
}
return File(info.OpenRead(), "text/plain");
}