|
今天在做一个页面的时候,需要选择行文件后,表头点击下载这个功能,
找了一个下载类用上编译都是一切正常,但是页面点击下载时出错,vs定位在 FineUI\Business\ResourceManager这个页面 “服务器无法在发送 HTTP 标头之后设置内容类型”
下面是上传类
public static void DownLoadFile(string filePath)
{
//string filePath = MapPathFile(FileName);
//指定块大小
long chunkSize = 204800;
//建立一个200K的缓冲区
byte[] buffer = new byte[chunkSize];
//已读的字节数
long dataToRead = 0;
FileStream stream = null;
try
{
//打开文件
stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
dataToRead = stream.Length;
//添加Http头
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachement;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath)));
HttpContext.Current.Response.AddHeader("Content-Length", dataToRead.ToString());
while (dataToRead > 0)
{
if (HttpContext.Current.Response.IsClientConnected)
{
int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));
HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
buffer = new Byte[chunkSize];
dataToRead = dataToRead - length;
}
else
{
//防止client失去连接
dataToRead = -1;
}
}
}
catch (Exception ex)
{
throw ex;
//HttpContext.Current.Response.Write("Error:" + ex.Message);
}
finally
{
if (stream != null)
{
stream.Close();
}
HttpContext.Current.Response.Close();
}
}
本人是刚入行的小白,求具体点的指教
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|