Downloading files from FTP

4

I need to download txt files that are in an FTP, I tried to use the following code apprehended 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 do not want to set a fixed path and I want it to take the original file name.

I want the download to be done the same when you download a file, such as from that site: link through Google Chrome

Does anyone please know 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.

    
asked by anonymous 18.11.2015 / 14:01

2 answers

1

I found a solution

I do not know if this solution is the right one but it does what I need:

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();
} 
    
04.12.2015 / 16:46
0

It's not the most beautiful way, 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());
}
    
18.11.2015 / 14:12