How to Download FTP Files

Asked

Viewed 756 times

4

I need to download txt files that are on an FTP, I tried to use the following repackaged code below for this but it does not do what I need.

The first parameter of the Downloadfile method is the URL I have, the second parameter is required but I don’t want to set a fixed path and I want it to take the original name of the file.

I want the download to be done the same when you download a file such as this site: https://jquery.com/download/ through the Google Chrome

Someone please knows how it can be done?

string url = "http://MeuSite/arquivo.text";
using (WebClient wc = new WebClient())
{
    wc.DownloadFile(url, "Não sei o que colocar aqui");
}

Thank you all.

  • I do not understand very well what you want to do. It is possible to give a more detailed explanation in the question?

  • if I’m not mistaken the first parameter of the Downloadfile method is the url, and the second is the file name

  • It’s simple, I have a grid with a list of files that are on FTP to download, when the user click to download a file he has to download to the user’s machine. Enter this jquery link and download it using Google’s browser, that’s exactly what I need. You got it?

  • Oh yes, I got it. I hadn’t seen the tag [tag:Asp.net]. Its only problem is what you want to take the original file name to save to the client, right?

  • Right, but I don’t want to set a fixed path, I want it to do the same as Chrome by downloading the file and saving in the user’s download folder logged in the machine.

  • @Mauricioferraz posts his solution as answer, then you can accept it as solution and the question will not go unanswered. :)

Show 1 more comment

2 answers

1


I found a solution

I don’t know if this solution is the right one but she does what I needed:

string strCaminho = "http://MeuSite/MeuArquivo.txt";
string nomeDoArquivo = "MeuArquivo.txt";

using (WebClient wc = new WebClient())
{    
    byte[] bytesFile = wc.DownloadData(strCaminho);

    Response.Clear();
    Response.ClearHeaders();
    Response.ClearContent();
    Response.ContentType = "text/plain";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + nomeDoArquivo + ";");
    Response.OutputStream.Write(bytesFile, 0, bytesFile.Length);
    Response.Flush();
    Response.Close();
} 

0

It’s not the most beautiful shape, but you already have the file name in your url, so you can split the url and get the file name. Remember that in the second parameter you also define the location that will be saved.

string url = "http://MeuSite/arquivo.text";
var arrayUrl = url.Split('/');            

using (WebClient wc = new WebClient())
{
    wc.DownloadFile(url,  @"c:\ " + arrayUrl.Last());
}
  • 1

    Just for the sake of curiosity, arrayUrl[arrayUrl.Length - 1] may be replaced by arrayUrl.Last() (using Linq).

  • This way it doesn’t go right, I don’t want to set the way c:, I want it to do like this jquery link, always saving in the logged user’s download folder.

  • @Mauricioferraz just search the location you want and change.

  • 1

    @Pedrocamarajunior The method Last() is an extension method of System.Linq, so you need to import this namespace to use the function Last(). My comment was a statement and not a question :p

  • If he doesn’t pass any path, he won’t even compile.

  • 1

    @Look, Mauricioferraz, I meant: if you pass only one filename, without the C:. In which location it tries to save?. I didn’t mean to take the parameter.

  • @jbueno disregard what he said then... Thanks for the tip!

  • @jbueno if you do not put the saved PATH in the project folder.

  • I’ll try that way, without the c:\

  • @Pedrocamarajunior But it’s a web project, how it goes salvar na pasta do projeto?

  • @jbueno Bah, true. I’m testing on console application. = / Anyway the path was pointed out, I think.

  • I appreciate all your help.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.