簡體   English   中英

使用Base64 String在MVC Webgrid中下載問題

[英]Download issue in MVC Webgrid using Base64 String

我正在嘗試在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格式。

請協助我解決問題。

提前致謝。

你需要為你的WebGrid添加一個額外的列,它將有一個名為“下載”的鏈接。這個鏈接需要在控制器中調用下載操作並通過被點擊的學生的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>)
    ))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM