How to download a file thru Ftp using Asp.Net/Visual Basic (visual studio 2008)

I give access to a multimedia module to clients that paid a 5 day subscription. This is part is working.
I am using an external hosting so my server does not slow down/saturate. I am using ftp with username and password, but I am using an asp link button (to hide link) wich uses response.redirect(directlink). But need to change this for 2 reasons.


Security... a curious client can somehow check the link and give it to any other client (they have to pay for access).


Internet Explorer (i'm using ver 8) does not allow clients to download with username and password directly (for reason 1)
...and, fo course, i can not tell them to use my preferred browser, they will probably stick with Internet Explorer.

I have tried several examples found over the internet, and have googled-it enough times, no luck.

Mostly using streamwriter and streamreader, but the examples need a location to download, and i would like the client to click, get the Save dialog, you know, the usual easy download... in other words, let the client's operating system take care of download location, not my app; my app will make the connection to the server, check if there's a file (haven't done this either) and start download if it does.
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

SunnyDarkConnect With a Mentor Commented:
The problem is as follows:
Whenever you use WebClient or any of the xxxWebRequests you ARE streaming content through your server, since the content gets downloaded to your server first and then streamed to the client computer. I believe that is not what you want, otherwise you would just store the files on your webserver.
So what yo want is to use response.redirect, however that will disclose the real URL including password and username to the client , since the file will be downloaded directly by the client's browser so the browser will have to know the actual URL of the FTP. So the conclusion is, you will not be able to do what you want the way you are trying to with external FTP. I would suggest using some other alternative service to host the files, one that will allow you to create temporary expiring links. One such service is Amazon S3:
The .NET API is very simple and allows you to create temporary signed URLs with expiration date in one simple call:
AmazonS3Client s3 = new Amazon.S3.AmazonS3Client(awskey,awssecret);
string url = s3.GetPreSignedURL(new GetPreSignedUrlRequest
   BucketName = "the name of the folder",
   Key = "the name of the file",
   Expires = new DateTime(2011, 6, 12)

Open in new window

I could suggest the following:
Client clicks a link something like : downloadfile.aspx?fileid=11111

The Page_Load event of downloadfile.aspx then checks the clients credentials and if it's a registered client reads the file from the disk and sends it to the Response stream.
I havent touched VB for a while so I'll just pseudo code, should be hard to translate
public Page_Load(e)
  if (Page.User.IsRegistered) 
     FileStream readin = File.Open(GetFileLocation[Request[fileid]))
     ArrayOfBytes data[4096]
     while ( not readin.EndOfFile)
         int readbytes = readin.Read(data)

Open in new window

note manage the value of strFilelocation, strFileName, strContenttype

Private void FileLoad()
               FileStream fs = File.Open(strFilelocation)

              Response.Buffer = true;
             Response.ContentType = strContentType;
             Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
             byte[] filedata;
              int temp = fs.Read(filedata)

The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

jmnfAuthor Commented:
SunnyDark and KetGuru

I believe i have tried both of this solutions before, i will try them again and report back... the problem with this 2 solutions is that my file is on an external ftp server, and also, it requires credentials. On the other hand, 1 of my files is 2.3 GB and 8 files of around 500 MB.

I know how to download from webapp server using response.transmitfile for sending very large files, but the external ftp server and the use of credentials (hidden credentials) is another problem.

I'll report back.
It is too big file. is not recommended option in this case than. I would suggest to use some batch file with ftp command and call/run that file from if you are keen to execute from only.
Also don't forget to pay attention to Server.ScriptTimeout property to allow the needed time to download and stream the file.
jmnfAuthor Commented:
I have been using the following procedure in the past to download a DVD image (.ISO) of 2.3 GB size and it works with

<httpRuntime executionTimeout="21600"/>

The only problem is that I had to remove the files from my onw server and now they are in a remote server, and I download them thru ftp (direct link), because the downloads were eating our bandwidth.

    Private Sub largeFileDownload(ByVal largeFile As String)
        Dim path As String = Server.MapPath(largeFile)
        Dim file As System.IO.FileInfo = New System.IO.FileInfo(path)

        If file.Exists Then
            Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name)
            Response.AddHeader("Content-Length", file.Length.ToString())
            Response.ContentType = "application/octet-stream"
        End If

    End Sub

Open in new window

So you see it is possible, but I need to have the ability to download them with network credentials (from the remote server) and that way avoid using a direct link.
jmnfAuthor Commented:
Thanks for the time and effort you are putting into this
jmnfAuthor Commented:
Forgot to tell you...

i am currently downloading them with the current code inside a link button click:

dim sFullURL as string = ""

Open in new window

but like i said on the original post, Internet Explorer won't let me download like this... and in firefox and some other browsers, if there is any error, it will show a page with the actual link, showing username and password.
If you would download the file to your server first and then stream it to the user then its no use keeping the file on a separate server. It would double you bandwidth usage instead.

Do you have the ability to generate temporary username passwords?
jmnfAuthor Commented:
I do not stream those files thru my web server, they get downloaded directly; i would have noticed the lag on my server.

I can't create temporary usernames and passwords because the ftp server is in a hosting service (wich i administer thru cPanel), i can create manually those usernames and passwords, so creating credentials for each client on my server is possible, but I would have to create 20 or 30 username and passwords daily on the remote FTP server (in the hosting service) for the sole purpose of letting the clients download with their temporary credentials.

When testing some procedures wich require a path on the client computer to save the file, I receive an Access Denied error, wich I believe is caused by the procedure trying to "write" the file on the server, instead of doing it on the computer I'm testing it on. I believe, then, that the connection to the FTP server is being made correctly, but I can't seem to figure out the problem of saving the file. I'm stuck... I have been looking and looking and looking with no luck... last time I tried a procedure with WebClient instead of FtpWebRequest / FtpWebResponse.

Help anyone, please. Some other ideas too, thanks.
jmnfAuthor Commented:
Thank you for your effort, I am currently looking for workarounds, as there is no easy solution as you explained.
All Courses

From novice to tech pro — start learning today.