Question Baffled by results of downloadfile --- need suggestions

oldpl1guy

Member
Joined
Jun 27, 2009
Messages
13
Programming Experience
1-3
Microsoft Visual Web Developer 2008 Express Edition. Behind code is VB.net

I am using DownLoadFile to copy a file from my website to my local PC. I have enclosed the process in try/catch/end try. I receive no errors. Upon completion of the process I check for the existence of the file at copyto from within the code. The result is that it does exist. As a further test, I open the file, read to completion and then place one of the lines from the read file in a label on the webform. Everything appears positive except the file does not really exist at the copyto location. I have displayed the value in the copyto variable and it is where I expected it to be.
copyfrom is set as copyfrom = Server.MapPath("/Set Directory/")
copyto is set as copyto = "c:\Set Directory\"
filename is selected from a DropDownBox that contains the names of the files on the website (copyfrom).

The relevant code is:
Sub downloadit()
Dim ps As New PermissionSet(PermissionState.None)
Dim fileReader As New WebClient()
fileReader.Credentials = CredentialCache.DefaultNetworkCredentials
fileReader.Headers.Add(HttpRequestHeader.UserAgent, "anything")
ps.AddPermission(New EnvironmentPermission(EnvironmentPermissionAccess.AllAccess, (copyfrom) & (filename)))
ps.AddPermission(New EnvironmentPermission(EnvironmentPermissionAccess.AllAccess, (copyto) & (filename)))
ps.AddPermission(New FileIOPermission(FileIOPermissionAccess.AllAccess, (copyfrom) & (filename)))
ps.AddPermission(New FileIOPermission(FileIOPermissionAccess.AllAccess, (copyto) & (filename)))

filename = (filename) & ".TXT"
Try
fileReader.DownloadFile((copyfrom) & (filename), (copyto) & (filename))
If System.IO.File.Exists((copyto) & (filename)) Then
Label1.Text = "Successfully Copied " + copyfrom + (filename) + " " & (copyto) & (filename)
readit()
End If
Catch ex As Exception
Label1.Text = "Error copying the file" + copyfrom + (filename) + " " & (copyto) & (filename) & " - " + ex.Message
End Try
end sub

Sub readit()
ln = 0
FileOpen(1, ((copyto) & (filename)), OpenMode.Input)
Do Until EOF(1)
ln = ((ln) + 1)
testline(ln) = LineInput(1).Trim
Loop
FileClose(1)
FileOpen(1, ((copyto) & "2" & (filename)), OpenMode.Output)
For a = 1 To (ln)
PrintLine(1, (testline(a)))
Label1.Text = (testline(2))
Next
FileClose(1)
End Sub

I am able to copy the file from one folder to another on the website with no problems.
I can't figure out why the code is able to read the file at the copyto location even though it is not actually there. I am baffled!

Any suggestions would be welcome.
 
That code is being executed on the web server. The path "C:\Set Directory\" refers to the C: drive of the web server, not the client running the browser. DownloadFile is a method that you would run in, say, a Windows Forms client application to download a file from a remote server. You don't call DownloadFile on the server to push a file to a client. You need to completely rethink your method.

download file asp.net - Google Search
 
Many thanks jmcilhinney. I'll rethink the process. How would one change the "c:/" identity to indicate the local PC rather than the website?
BTW, I can't believe that my ISP is allowing me to write to the website c drive, but that is obviously happening.
 
You don't indicate the local PC. Your web site doesn't know anything about the client other than what's in the request. Your web site just makes the file available and the client downloads it. The client then decides where to save the downloaded file, just as you do whenever you download a file through your own browser.
 
Back
Top