string fileURL = this.Server.MapPath("你烂首要下载的文件路径")//文件路径饥李数,可用相对路径
FileInfo fileInfo = new FileInfo(fileURL)
Response.Clear()
Response.AddHeader("content-disposition", "attachmentfilename=" + Server.UrlEncode(fileInfo.Name.ToString()))//扰卜文件名
Response.AddHeader("content-length", fileInfo.Length.ToString())//文件大小
Response.ContentType = "application/octet-stream"
Response.ContentEncoding = System.Text.Encoding.Default
Response.WriteFile(fileURL)
天下数据--专业运营香港服务器、韩国服务器、美国服务器等等海外优质服务器!
private void ResponDown(string fileName,string filepatch){
Response.Clear()
Response.ClearHeaders()
Response.Buffer = false
Response.ContentType = "application/并培ms-excel"
Response.AppendHeader("Content-Disposition", "attachmentfilename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8))
//Response.Write(writer)
FileStream file = new FileStream(filepatch, FileMode.Open, FileAccess.Read, FileShare.Read)
BinaryReader br = new BinaryReader(file)
Response.AppendHeader("Content-Length", file.Length.ToString())
// StringReader sr = new StringReader(writer.GetStringBuilder().ToString())
long flen =file.Length
int size = 102400/罩蚂/每100k同时下载数据
byte[] readdata = new byte[size]/物蔽埋/指定缓冲区的大小
if (size >flen) size = Convert.ToInt32(flen)
long fpos = 0
bool isend = false
while (!isend)
{
if (Response.IsClientConnected)
{
if ((fpos + size) >flen)
{
size = Convert.ToInt32(flen - fpos)
readdata = new byte[size]
isend = true
}
if (size >1)
{
br.Read(readdata, 0, size)//读入一个压缩块
// byte[] re = Encoding.UTF8.GetBytes(readdata, 0, readdata.Length)
Response.BinaryWrite(readdata)
//Response.OutputStream.Write(re, 0, size)
}
fpos += size
}
else
{
Response.End()
}
}
file.Close()
Response.Flush()
Response.End()
}
给你个下载函数
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)