gpt4 book ai didi

c# - 使用 Base64 字符串在 MVC Webgrid 中下载问题

转载 作者:行者123 更新时间:2023-11-30 12:56:51 28 4
gpt4 key购买 nike

我正在尝试在 MVC 网络网格中实现下载功能。这是演示代码。

型号

 public class Student
{
public string Name { get; set; }
public string Address { get; set; }

public string ByteArray { get; set; }

public List<Student> StudentList { get; set; }

}

查看

 @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") ))

Controller

 public ActionResult Index()
{
Student obj = new Student();
obj.StudentList = new List<Student>();
return View(obj);

}
[HttpPost]
public ActionResult Index(Student obj, HttpPostedFileBase file1)
{
if (file1 != null)
{
MemoryStream target = new MemoryStream();
file1.InputStream.CopyTo(target);
byte[] data = target.ToArray();
obj.ByteArray = Convert.ToBase64String(data);
}

obj.StudentList = new List<Student>();
obj.StudentList.Add(obj);
return View(obj);

}

在此代码中,我尝试在 MVC Web 网格中显示学生详细信息以及文件下载选项。

但是在这里,我不知道如何下载 base64string 格式的文件。

请协助我解决问题。

提前致谢。

最佳答案

您需要向您的 WebGrid 添加一个额外的列,它将有一个名为“下载”的链接。此链接需要在 Controller 中调用下载操作并传递被点击对象的 base64 属性student.Below 是一个完整的工作示例,希望对您有所帮助:

Controller :

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>)
))

关于c# - 使用 Base64 字符串在 MVC Webgrid 中下载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37941098/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com