ASP.NET MVC: C# Download file and save as dialog
This question already has an answer here:
Given that is an Excel doc saved at c:tempexcelDoc.xls and given that you have a WebForm that has a link like this
<asp:LinkButton runat="server" ID="GetExcel" OnClick="GetExcel_Click">Download</asp:LinkButton>
In your code-behind you can read the file from disk and send to the user by something like this
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/46834.html
上一篇: phpexcel下载