下載,直接通過(guò)url讀取文件,然后Response.OutputStream.Write()數(shù)據(jù)
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),潘集企業(yè)網(wǎng)站建設(shè),潘集品牌網(wǎng)站建設(shè),網(wǎng)站定制,潘集網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,潘集網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專(zhuān)業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
下面提供個(gè)下載的靜態(tài)方法,是C#的,供參考:
///?summary
///?下載文件
///?/summary
///?param?name="fileName"下載的文件名稱(chēng)(包括擴(kuò)展名)/param
///?param?name="filePath"下載文件的絕對(duì)路徑/param
public?static?void?DownFile(string?fileName,?string?filePath)
{
//打開(kāi)要下載的文件,并把該文件存放在FileStream中????????????????
System.IO.FileStream?Reader?=?System.IO.File.OpenRead(filePath);
//文件傳送的剩余字節(jié)數(shù):初始值為文件的總大小????????????????
long?Length?=?Reader.Length;
HttpContext.Current.Response.Buffer?=?false;
HttpContext.Current.Response.AddHeader("Connection",?"Keep-Alive");
HttpContext.Current.Response.ContentType?=?"application/octet-stream";
HttpContext.Current.Response.Charset?=?"utf-8";
HttpContext.Current.Response.AddHeader("Content-Disposition",?"attachment;?filename="?+?System.Web.HttpUtility.UrlEncode(fileName));
HttpContext.Current.Response.AddHeader("Content-Length",?Length.ToString());
byte[]?Buffer?=?new?Byte[10000];//存放欲發(fā)送數(shù)據(jù)的緩沖區(qū)????????????????
int?ByteToRead;?//每次實(shí)際讀取的字節(jié)數(shù)???????????????
while?(Length??0)
{????
//剩余字節(jié)數(shù)不為零,繼續(xù)傳送????????????????????
if?(HttpContext.Current.Response.IsClientConnected)
{????
//客戶端瀏覽器還打開(kāi)著,繼續(xù)傳送????????????????????????
ByteToRead?=?Reader.Read(Buffer,?0,?10000);???????????????????//往緩沖區(qū)讀入數(shù)據(jù)????????????????????????
HttpContext.Current.Response.OutputStream.Write(Buffer,?0,?ByteToRead);????
//把緩沖區(qū)的數(shù)據(jù)寫(xiě)入客戶端瀏覽器????????????????????????
HttpContext.Current.Response.Flush();?//立即寫(xiě)入客戶端????????????????????????
Length?-=?ByteToRead;//剩余字節(jié)數(shù)減少????????????????????????????}
else
{?????????????????????????
//客戶端瀏覽器已經(jīng)斷開(kāi),阻止繼續(xù)循環(huán)????????????????????????
Length?=?-1;
}
}????????????????//關(guān)閉該文件???????????????
Reader.Close();
}
'緩沖區(qū)長(zhǎng)度
Const BufLen As Integer=800
'包發(fā)送間隔
Const Interval As Integer=62
'緩沖區(qū)
Dim buf As Byte()=Nothing
'遠(yuǎn)程(目的)網(wǎng)絡(luò)端點(diǎn)
Dim remoteep As IPEndPoint=Nothing
'如果你已將s定義為類(lèi)的成員變量(實(shí)例或共享),注釋掉下面這句
Dim s As UdpClient=Nothing
Dim willread As Integer
Try
buf=New Byte(BufLen-1) {}
s=New UdpClient()
' TextBox1.Text包含遠(yuǎn)程(目的)主機(jī)的IP地址
' TextBox2.Text包含遠(yuǎn)程(目的)主機(jī)的端口號(hào)
remoteep=New IPEndPoint(IPAddress.Parse(Trim(TextBox1.Text)),CInt(TextBox2.Text))
' Label2.Text包含要發(fā)送的文件的路徑
Using fs As New FileStream(Label2.Text,FileMode.Open,FileAccess.Read)
While fs.Positionfs.Length
willread=BufLen
If fs.Length-fs.PositionBufLen Then
willread=CInt(fs.Length-fs.Position)
End If
fs.Read(buf,0,willread)
s.Send(buf,willread,remoteep)
Thread.Sleep(Interval)
End While
End Using
Catch ex As Exception
MsgBox(ex.ToString())
Finally
If s IsNot Nothing Then
s.Close()
End If
End Try
把結(jié)果用字節(jié)集變量,再其字節(jié)集數(shù)據(jù),長(zhǎng)整數(shù)就行了,多大都能取