string m_fileName = string.Empty;
string m_filePath = HttpContext.Current.Request.PhysicalApplicationPath + "Template\\ContacterList.xls";
m_fileName = Path.GetFileName(m_filePath);
System.IO.Stream iStream = null;
byte[] buffer = new byte[10000];
int length;
long dataToRead;
try
{
iStream = new System.IO.FileStream(m_filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
dataToRead = iStream.Length;
Response.ContentType = "application/save";
Response.AddHeader("Content-Disposition", "attachment;filename=" + m_fileName);
Response.AddHeader("Content-Length", dataToRead.ToString());
while (dataToRead > 0)
{
if (Response.IsClientConnected)
{
length = iStream.Read(buffer, 0, 10000);
Response.OutputStream.Write(buffer, 0, length);
Response.Flush();
buffer = new byte[10000];
dataToRead = dataToRead - length;
}
else
{
dataToRead = -1;
}
}
}
catch (Exception ex)
{
Response.Write("Error:" + ex.Message);
Response.End();
}
finally
{
if (iStream != null)
{
iStream.Close();
}
}
<asp:LinkButton ID="lbtnTemplate" runat="server" Text="点击此处下载上传的模板" OnClick="lbtnTemplate_Click" />