簡體   English   中英

如何檢索和下載服務器文件(File.Exists和URL)

[英]How to retrieve and download server files (File.Exists and URL)

我有一個數據庫表,用戶在其中標記要下載的文件。 隨后,我瀏覽該表並需要創建一個fileList傳遞給ActiveX下載器。 我的例程僅在第一個文件在本地和服務器上運行。 我知道我的邏輯一定不好,但是我找不到。 所有這些文件始終位於同一服務器目錄中,如下所示:“ D:\\ inetpub \\ vhosts \\ WebSite.com \\ sessionVideos \\”

Sub (GetFileList)
Dim dtVideosSelected As New DataTable
Dim drVideosSelected As New DataRow
Dim strSourceDirectory As String = "sessionVideos/"
Dim strServerBasePath As String = Server.MapPath(strSourceFileDirectory)
Dim strFileName As String
Dim fileInfo As System.IO.FileInfo
Dim i As Int16

Response.Clear()
Response.ContentType = "text/plain"
Response.Charset = "UTF-8"

i = 0
Do While i < dt VideosSelected.Rows.Count
    drVideosSelected = dtVideosSelected.Rows(i)
    strFileName = drVideosSelected("VID_FileName")
    If File.Exists(strServerbasePath & strFileName)
        fileInfo = New System.IO.FileInfo(strServerbasePath & strFileName)
        Response.Write("*/* | " & fileInfo.Length & " | " & fileInfo.Name & " | ")
        Response.Write(EncodeFileName(strSourceFileDirectory & fileInfo.Name) & vbCr
            & vbLf)
    End If
    i += 1
Loop

Response.End()
Response.Flush()
End Sub

Private Function EncodeFileName(ByVal fullPath As String) As String
    Return Server.UrlEncode(fullPath).Replace("+", "%20").Replace("%2f", "/")
End Function

我嘗試了很多不同的嘗試,但都沒有成功。

詹姆士

要下載文件,

Dim b() As Byte = System.IO.File.ReadAllBytes(strServerbasePath & strFileName)
Response.Clear()
Response.ClearHeader()
Response.AddHeader("Content-Type: application/octate-stream")
Repponse.AddHeader("Content-Length: " & b.Length)
Response.AddHeader("Content-Disposition: attachment; filename=" & strFileName)
Response.BinaryWrite(b)
Response.Flush()
Response.End()

好的,我們有一個答案。...實際上,我在無法使用的文件上有兩個文件擴展名,但是隱藏了已知擴展名的選項已打開(我猜默認情況下)。 .avi文件看起來與其他文件相同,因此我想它正在考慮將.avi視為“未知”文件類型。 隨你!

答案在我的其他帖子上:

為什么FileInfo顯示額外的文件擴展名?

謝謝,詹姆斯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM