ASP.NET MVC:C#下载文件并另存为对话框

这个问题在这里已经有了答案:

  • 为Excel文档设置MIME类型5个答案

  • 鉴于这是一个保存在c: temp excelDoc.xls中的Excel文档,并且假设您有一个WebForm,它有一个像这样的链接

    <asp:LinkButton runat="server" ID="GetExcel" OnClick="GetExcel_Click">Download</asp:LinkButton>
    

    在您的代码隐藏中,您可以从磁盘读取文件并通过类似的方式发送给用户

        protected void GetExcel_Click(object sender, EventArgs e)
        {
            var fileName = "excelDoc.xls";
            using (var cs = new FileStream(@"c:temp" + fileName, FileMode.Open))
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
                Response.ContentType = "application/vnd.ms-excel";
    
    
                byte[] buffer = new byte[32768];
                int read;
                while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)
                {
                    Response.OutputStream.Write(buffer, 0, read);
                }
    
                Response.End();
                Response.Flush();
            }
        }
    
    链接地址: http://www.djcxy.com/p/46833.html

    上一篇: ASP.NET MVC: C# Download file and save as dialog

    下一篇: Disposition in HTTP Header (c#)