asp.net下载网络文件
发布日期:2021-05-14 04:36:45 浏览次数:17 分类:博客文章

本文共 4370 字,大约阅读时间需要 14 分钟。

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������ ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

private
bool
DownLoadFile() {
//
������������ ��� ������������
string
filePath
=
Server.MapPath(
"
~/DownLoad/myFiles
"
);
string
downLoadFile
=
"
test.mkv
"
; FileInfo fileName
=
new
FileInfo(filePath
+
"
\\
"
+
downLoadFile); FileStream fileStream
=
new
FileStream(filePath
+
"
\\
"
+
downLoadFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//
���������
BinaryReader binaryReader
=
new
BinaryReader(fileStream);
//
������������������
if
(fileName.Exists) {
try
{
long
startBytes
=
0
;
string
lastUpdateTimeStamp
=
File.GetLastWriteTimeUtc(filePath).ToString(
"
r
"
);
string
encodeData
=
HttpUtility.UrlEncode(downLoadFile, Encoding.UTF8)
+
lastUpdateTimeStamp;
//
������response���������
Response.Clear(); Response.Buffer
=
false
; Response.AddHeader(
"
Accept-Ranges
"
,
"
bytes
"
); Response.AppendHeader(
"
ETag
"
,
"
\"
"
+
encodeData
+
"
\"
"
); Response.AppendHeader(
"
Last-Modified
"
, lastUpdateTimeStamp);
//
������contenttype
Response.ContentType
=
"
application/octet-stream
"
;
//
���������������������������������������������������������������
Response.AddHeader(
"
Content-Disposition
"
,
"
attachment;filename=
"
+
fileName.Name); Response.AddHeader(
"
Content-Length
"
, (fileName.Length
-
startBytes).ToString()); Response.AddHeader(
"
Connection
"
,
"
Keep-Alive
"
);
//
������������������
Response.ContentEncoding
=
Encoding.UTF8;
//
������������
binaryReader.BaseStream.Seek(startBytes, SeekOrigin.Begin);
//
������������������1024������������������
int
maxCount
=
(
int
)Math.Ceiling((fileName.Length
-
startBytes
+
0.0
)
/
1024
);
//
���1024������������������������
int
i;
for
(i
=
0
; i
<
maxCount
&&
Response.IsClientConnected; i
++
) { Response.BinaryWrite(binaryReader.ReadBytes(
1024
)); Response.Flush(); }
//
���������������������������������������������������������������������
if
(i
<
maxCount)
return
false
;
return
true
; }
catch
{
return
false
; }
finally
{
//
������������������������������
Response.End(); binaryReader.Close(); fileStream.Close(); } }
else
{ ScriptManager.RegisterStartupScript(
this
, GetType(),
"
���������������
"
,
"
alert('������������������')
"
,
true
); }
return
false
; }

���������������������������������������������������������

上一篇:WinForms下的SliderButtons设计
下一篇:GridView实现多表头合并

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2025年05月03日 19时53分11秒