public void DownloadFile(string fileName)
{
string adminUploadFilePath = ConfigParameter.AdminUploadFilePath;
if (!string.IsNullOrEmpty(fileName))
{
string filePath = Path.Combine(string.Format(@"{0}/AlumniCommunication/files", adminUploadFilePath), string.Format(@"{0}", fileName));
FileInfo fileInfo = new FileInfo(filePath);//D:\项目\BHG\BHGAlumni\AlumniAssociation.Admin\Upload/AlumniCommunication/files\济大各地校友会.xlsx
if (fileInfo.Exists == true) //如果存在文件
{
byte[] buffer = new byte[1024 * 1024]; //每次读取1024 * 1024大小的数据
Response.Clear();
FileStream fs = File.OpenRead(filePath);
long dataToRead = fs.Length;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName));
Response.AppendHeader("Content-Length", fs.Length.ToString());
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
while (dataToRead > 0)
{
int readLength = fs.Read(buffer, 0, buffer.Length);
Response.OutputStream.Write(buffer, 0, readLength);
Response.Flush();
dataToRead = dataToRead - readLength;
}
Response.Flush();
fs.Dispose();
Response.Close();
}
else
{
Response.Write("<script>alert('没有找到对应文件');</script>");
}
}
}
{
string adminUploadFilePath = ConfigParameter.AdminUploadFilePath;
if (!string.IsNullOrEmpty(fileName))
{
string filePath = Path.Combine(string.Format(@"{0}/AlumniCommunication/files", adminUploadFilePath), string.Format(@"{0}", fileName));
FileInfo fileInfo = new FileInfo(filePath);//D:\项目\BHG\BHGAlumni\AlumniAssociation.Admin\Upload/AlumniCommunication/files\济大各地校友会.xlsx
if (fileInfo.Exists == true) //如果存在文件
{
byte[] buffer = new byte[1024 * 1024]; //每次读取1024 * 1024大小的数据
Response.Clear();
FileStream fs = File.OpenRead(filePath);
long dataToRead = fs.Length;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName));
Response.AppendHeader("Content-Length", fs.Length.ToString());
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
while (dataToRead > 0)
{
int readLength = fs.Read(buffer, 0, buffer.Length);
Response.OutputStream.Write(buffer, 0, readLength);
Response.Flush();
dataToRead = dataToRead - readLength;
}
Response.Flush();
fs.Dispose();
Response.Close();
}
else
{
Response.Write("<script>alert('没有找到对应文件');</script>");
}
}
}