0

I have a database table where the user marks files to be downloaded. Subsequently, I browse this table and need to create a fileList to pass to an ActiveX downloader. My routine works locally and on the server for ONLY the first file. I know my logic must be bad, but I cannot find it. All of these files are always in the same server directory which is as follows: "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

I have tried a lot of different things with no success.

James

RAS
  • 8,100
  • 16
  • 64
  • 86
James
  • 305
  • 4
  • 10
  • 23
  • When you say it doesn't work do you mean the activex controll isn't downloading the files or did you actually check the Response being returned to the browser (with something like fidler or the net tab in firebug) and see the output (if so please include it). Also what happens when you step trough the code with a debugger by setting a breakpoint on the i = 0 statement and running step by step from there? You should be able to see what it does. If row count is only 1 or if the file exists returns false etc... – olle Sep 06 '09 at 01:43
  • Reading your comment to adatapost his/her suggestion I now understand that it works fine locally but not on the server. This would indicate that the problem isn't in the logic of your code. But rather in a different between the local and server configuration. Please check the http response both locally and on the server. Including the headers and compare those two. – olle Sep 06 '09 at 01:45
  • Sorry, I wasn't clear enough. My test situation is that I have three files tagged for download (confirmed the query returns this). My loop here is only returning one file instead of the three it should. Thank you - James – James Sep 06 '09 at 02:00
  • The loop seems fine. Does this problem occur on both locations or only on the remote one? – olle Sep 06 '09 at 02:08
  • Just on the server. This returns all three files locally. I can tell because the returned file list is visible in the downloader. I get all three files locally and only the first on the server. The files and directory are identical except of course the server path is differnt. The strange thing is why the first file works but not the others. – James Sep 06 '09 at 02:18
  • The "File Exists" has got to be returning false on the second and third files, but I can't find a good reason for that. – James Sep 06 '09 at 02:20
  • once again then I don't think it;s the code but a difference in the data or configuration. I would start by adding some logging statements that print out their location and the values of i, strFileName and File.Exists(strServerbasePath & strFileName) – olle Sep 06 '09 at 02:23
  • Well, I tried another test. More info... My first file is a .avi, the second is a .exe, and the third is a .txt. If I request either the .exe or .txt file ONLY (one file only through the query), neither one of them gets a hit on File Exists. I can't make sense of this. – James Sep 06 '09 at 02:25
  • Ok, makes sense.... I am not familiar with logging statements so will try to look that up. If you can provide a quick example, that would be great. Yes, I am a beginner! If not, I will find it. Thanks, James – James Sep 06 '09 at 02:27
  • Ok, I have found that I guess IE must be determining that the file extensions are not matching the file and must cause File.Exists to return false. I had just renamed some files to use for testing. I will have to try some real files created with various extensions. – James Sep 06 '09 at 04:30
  • See my new post for the problem http://stackoverflow.com/questions/1386338/why-is-fileinfo-showing-an-extra-file-extension – James Sep 06 '09 at 18:32

2 Answers2

0

To download a file,

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()
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • Thank you, but I don't need to know how to download a file. I am using a purchased ActiveX control to do the job. I am downloading multiple files and need to create the file list here to pass to the downloader. The problem is that it is only picking up the first file. I am sure there is an issue with my written logic here. Again, this works locally just fine but not on the server. Thanks, James – James Sep 06 '09 at 01:41
0

Ok, we have an answer.... I actually had double file extensions on the files that were not working but the option to hide known extensions was turned on (I guess by default). The .avi files looked the same as the others so I guess it was considering .avi to be an "unknown" file type. Whatever!

The answer is on my other post here:

Why is FileInfo showing an extra file extension?

Thanks, James

Community
  • 1
  • 1
James
  • 305
  • 4
  • 10
  • 23