我正在尝试在MVC网格中实现下载功能。这是演示代码。
模型
<ul>
<li ng-repeat="item in firstArray | limitTo:secondArray.length">{{item}}</li>
</ul>
查看
slices<-c(pu1_500,pu501_1000,pu1001_2000)
cols <- c("red","blue","green")
percentlabels<- round(100*slices/sum(slices), 1)
pie3D(slices,main="piechart",col=cols,labels=percentlabels,explode=0.1)
legend("topright", c("1-500","501-1000","1001-2000"), cex=0.8,fill=cols)
控制器
public class Student
{
public string Name { get; set; }
public string Address { get; set; }
public string ByteArray { get; set; }
public List<Student> StudentList { get; set; }
}
在此代码中,我试图在MVC Web网格中显示学生详细信息以及文件下载选项。
但是在这里,我没有得到如何下载base64string格式的文件。
请协助我解决问题。
先谢谢。
答案 0 :(得分:0)
您需要为WebGrid
添加一个额外的列,其中包含一个名为“下载”的链接。此链接需要在控制器中调用下载操作并通过单击的student.Below的base64属性。是一个完整的工作示例,希望它可以帮助您:
<强>控制器:强>
public class StudentController : Controller
{
public ActionResult Index()
{
Student obj = new Student();
byte[] bytes = System.IO.File.ReadAllBytes("F:\\test.txt");
string ba = Convert.ToBase64String(bytes);
var s1 = new Student { Name = "Student1", Address = "Address1", ByteArray = ba };
var s2 = new Student { Name = "Student2", Address = "Address2", ByteArray = ba };
var s3 = new Student { Name = "Student3", Address = "Address3", ByteArray = ba };
obj.StudentList = new List<Student>() { s1, s2, s3 };
return View(obj);
}
[HttpGet]
public FileResult DownloadAction(string file)
{
byte[] fileBytes = Convert.FromBase64String(file);
string fileName = "test.txt";
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
}
查看:强>
@model Fileuploaddemo.Models.Student
@{ var grid = new WebGrid(Model.StudentList);}
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)
@Html.LabelFor(model => model.Address)
@Html.EditorFor(model => model.Address)
@grid.GetHtml(
htmlAttributes: new
{
id = "gridT1",
@calss = ""
},
tableStyle: "webgrid-table",
headerStyle: "webgrid-header",
columns: grid.Columns(
grid.Column(columnName: "Name", header: "Name"),
grid.Column(columnName: "Address", header: "Address"),
grid.Column("Download", "Download", format: @<text>
@Html.ActionLink("Download", "DownloadAction", "Student", new { file = item.ByteArray },null)
</text>)
))