Downloading a file from an ftp server and prompting a savefiledialog

  • 13 years ago

    Hello,

     

    I am currently trying to make an asp page that will let a user download a file from an ftp server (ftp://xxxxxxx/XE700Attachments/). The list of files are displayed on a grid. I would like to give the user the capability to double click the row of the attachment, then a saveFileDialog pop so the user can choose where to save that file localy and then download such file from the ftp server.

    As background info. The ftp server is hosted in a Server 2003 machine different than the development one. However I am hosting the webpage on the server2003 not on the development one.

     I have tried two aproaches:

    1st. using WebClient I was having a security exeption (Access to the path 'C:\Carlos.txt' is denied.). Anyway I believe that this approach would not pop up a savefiledialog. Is that correct?

    here is the code: (AttachmentRelativePath is the relative path including the filename and extension on the ftp server)

    private void BeginFileDownload(string AttachmentRelativePath)

    {

    string LocalFilePath = @"C:\Carlos.txt";

    Configuration.GetConfiguration();

    WebClient Client = new WebClient();

    Client.Credentials = new NetworkCredential(Configuration.AdminUserName, Configuration.AdminUserPassword);

    Client.DownloadFile(Configuration.AttachmentsPathFTP + AttachmentRelativePath, LocalFilePath);

     

    2nd. Using Response I was having the following exeption: 

     [UriFormatException: Invalid URI: The format of the URI could not be determined.]
       System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind) +1674682
       System.Uri..ctor(String uriString) +17

    Here is the code:

     

    private void BeginFileDownload(string AttachmentRelativePath)

    {

    string FilePath = Server.MapPath(@"\XE700Attachments\");System.IO.FileInfo TargetFile = new System.IO.FileInfo(FilePath + AttachmentRelativePath);

    Response.Clear();

    Response.AddHeader(
    "Content-Disposition", "attachment; filename=" + TargetFile.Name);Response.AddHeader("Content-Length", TargetFile.Length.ToString());Response.ContentType = "application/octet-stream";

    Response.WriteFile(TargetFile.FullName);

    Response.End();

     

    I rather use webClient since I can handle credentials. Any idea what I am doing wrong? Is there a better way to acomplish this task?

    I will appreciate any input

     Thanks

    Carlos

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Anyone who considers arithmetic methods of producing random digits is, of course, in a state of sin.” - John von Neumann