Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-25
12
Medium Priority
?
923 Views
Last Modified: 2013-11-26
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.

1


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

2


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.
0
Comment
Question by:jmnf
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 8

Expert Comment

by:SunnyDark
ID: 34987566
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) 
  then
  {
     FileStream readin = File.Open(GetFileLocation[Request[fileid]))
     ArrayOfBytes data[4096]
     while ( not readin.EndOfFile)
     {
         int readbytes = readin.Read(data)
         Response.OutputStream.Write(data)
     }
     Response.Flush()
     Response.End()
  }
}

Open in new window

0
 
LVL 3

Expert Comment

by:KetGuru
ID: 34998039
note manage the value of strFilelocation, strFileName, strContenttype

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

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

        }
}
0
 
LVL 4

Author Comment

by:jmnf
ID: 34998517
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Expert Comment

by:KetGuru
ID: 34998629
It is too big file. asp.net 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 asp.net if you are keen to execute from asp.net only.
0
 
LVL 8

Expert Comment

by:SunnyDark
ID: 35001007
Also don't forget to pay attention to Server.ScriptTimeout property to allow the needed time to download and stream the file.
0
 
LVL 4

Author Comment

by:jmnf
ID: 35002577
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.Clear()
            Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name)
            Response.AddHeader("Content-Length", file.Length.ToString())
            Response.ContentType = "application/octet-stream"
            Response.TransmitFile(file.FullName)
            Response.End()
        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.
0
 
LVL 4

Author Comment

by:jmnf
ID: 35002579
Thanks for the time and effort you are putting into this
0
 
LVL 4

Author Comment

by:jmnf
ID: 35002620
Forgot to tell you...

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

dim sFullURL as string = "ftp://username:password@remoteserver.com/filename.iso"
response.redirect(sFullURL)

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.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35021336
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?
0
 
LVL 4

Author Comment

by:jmnf
ID: 35022946
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.
0
 
LVL 8

Accepted Solution

by:
SunnyDark earned 2000 total points
ID: 35024727
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: http://aws.amazon.com/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)
});
Response.Redirect(url);

Open in new window

0
 
LVL 4

Author Closing Comment

by:jmnf
ID: 35316042
Thank you for your effort, I am currently looking for workarounds, as there is no easy solution as you explained.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question