ASP.NET如何下载服务器端的文件?比如下载网站根目录下的“1.txt”到桌面上,求详细代码

ASP.NET如何下载服务器端的文件?比如下载网站根目录下的“1.txt”到桌面上,求详细代码,第1张

给你提供一点代码:

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()

}

给你个下载函数


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12264182.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存