【问题标题】:Download issue in MVC Webgrid using Base64 String使用 Base64 字符串在 MVC Webgrid 中下载问题
【发布时间】:2016-06-21 09:51:48
【问题描述】:

我正在尝试在 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") ))

控制器

 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格式的文件。

请帮助我解决问题。

提前致谢。

【问题讨论】:

    标签: c# asp.net asp.net-mvc


    【解决方案1】:

    您需要在WebGrid 中添加一个额外的列,其中会有一个名为“Download”的链接。此链接需要在控制器中调用下载操作并传递被点击学生的 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>)
        ))
    

    【讨论】:

    • 我已经尝试过了,但无法将文件作为路由值传递给 actionlink,因为它是以 Base64string 格式存储的。
    • 为什么不呢?我在上面的例子中通过了它,它工作得很好。无论如何,你总是可以将记录的 ID 传递给控制器​​,并在控制器中使用 ID 来查找base64字符串并进行下载,非常简单。只需在学生类中添加一个ID属性并将ActionLink更改为Html.ActionLink("Download", "DownloadAction", "Student", new { id = ID },null)
    猜你喜欢
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 2016-02-05
    • 1970-01-01
    • 2014-10-07
    相关资源
    最近更新 更多