vb中下載一個apk文件的代碼:

shuye 2012-05-03 03:30:02
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim filename As String = "PHRMOBILE.apk"


fileDownload(filename, Server.MapPath(filename))
End Sub

Private Sub fileDownload(ByVal fileName As String, ByVal fileUrl As String)


Page.Response.Clear()
Dim success As Boolean
success = ResponseFile(Page.Request, Page.Response, fileName, fileUrl, 1024000)
If Not success Then
Response.Write("Downloading Error!")
End If

Page.Response.End()
End Sub

Public Function ResponseFile(ByVal _Request As HttpRequest, ByVal _Response As HttpResponse, ByVal _fileName As String, ByVal _fullPath As String, ByVal _speed As Integer) As Boolean

Try


Dim myFile As FileStream = New FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
'Response.Write("<script>alert(' here ')</script>")
Dim br As BinaryReader = New BinaryReader(myFile)

Try
'
_Response.AddHeader("Accept-Ranges", "bytes")
_Response.Buffer = False
Dim fileLength As Long = myFile.Length
Dim startBytes As Long = 0


Dim pack As Integer = 10240

Dim sleep As Integer = CType(Math.Floor(1000 * pack / _speed) + 1, Integer)


If _Request.Headers("Range") <> Nothing Then
_Response.StatusCode = 206
Dim range() As String = _Request.Headers("Range").Split(New Char() {"="c, "-"c})
startBytes = Convert.ToInt64(range(1))

End If

_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString())

If startBytes <> 0 Then
_Response.AddHeader("Content-Range", String.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength))

End If



_Response.AddHeader("Connection", "Keep-Alive")
_Response.ContentType = "application/octet-stream"
_Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8))

br.BaseStream.Seek(startBytes, SeekOrigin.Begin)


Dim maxCount As Integer = CType(Math.Floor((fileLength - startBytes) / pack) + 1, Integer)

Dim i As Integer
'For i = 0 To maxCount - 1
For i = 0 To maxCount - 1 Step i + 1

If (_Response.IsClientConnected) Then

_Response.BinaryWrite(br.ReadBytes(pack))
Thread.Sleep(sleep)

Else

i = maxCount
End If
Next



'catch
'{
' return false;
'}
Catch ex As Exception
Return False

Finally
br.Close()
myFile.Close()
End Try



Catch ex As Exception

Return False


End Try

Return True


End Function
End Class
...全文
97 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
shuye 2012-05-07
  • 打赏
  • 举报
回复
嗯,但是就是不懂啊
crackdung 2012-05-03
  • 打赏
  • 举报
回复
安卓的,呵呵
shuye 2012-05-03
  • 打赏
  • 举报
回复
补充:请高手给我分析一下,这些代码什么意思吧,有好多看不懂的,谢谢哈!
急急急急!!!!

7,765

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧