pdf文件正在创建和下载..但无法打开,因为它显示无法打开文件

时间:2016-07-22 12:18:53

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

这是我的代码..

 [HttpPost]
    public ActionResult Export()
    {
        GridView grid = new GridView();
        StringWriter sw = new StringWriter();

        var ItemsFromDb = lst.FetchDataFromList(Session["userId"].ToString()).ToList();
        if (ItemsFromDb.Count != 0)
        {
            grid.DataSource = ItemsFromDb;
            grid.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("Content-disposition", "Attachment;filename=" + "Members_" + DateTime.Now.ToString("dd/mm/yyy") + ".pdf");
            Response.ContentType = "application/pdf";
            Response.Charset = "";
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            grid.RenderControl(hw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
        else
        {
            TempData["Message"] = "Sorry.. No data to export.";
            return View();
        }
        return RedirectToAction("UserDetails","EmployeeLocation");
    }

此代码仅适用于创建和下载Excel格式,仅将类型更改为.xls"application/ms-excel"

但是数据没有加载的是pdf ...

0 个答案:

没有答案