Dr developer
Dr developer

Reputation: 81

How to send FTP file to client browser?

i wrote asp.net Webservice to generate ftp download link and send it to client browser, so client can download it. the link is like this:

<a href='ftp://test:test@10:10:10:10:21/test.txt'>download</a>

i send that link trough Ajax response call.

so how can i prevent users to view my ftp user and password? or how to encrypt link?

approach #2:

to avoid above link i wrote code to send chunk of data but nothing is happening in browser. (I am using fluentftp library . )

[WebMethod(enableSession: true)]
public void GetDownload(string brandName, string modelName, string osName, string file)
{
  if (!FtpConnect())
            throw new Exception("Error ftp connection.");
  byte[] buffer = new byte[1024];
  string path = "disk1/Drivers/" + GetFtpBrands(brandName) + "/" + modelName + "/" + osName + "/"  +file;
  HttpContext.Current.Response.Clear();
  HttpContext.Current.Response.ClearContent();
  HttpContext.Current.Response.BufferOutput = true;
  HttpContext.Current.Response.ContentType = "application/octet-stream";
  HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + file);
  FtpDataStream inputStream = (FtpDataStream) ftp.OpenRead(path,FtpDataType.Binary);

  int read = 0;
  while ((read = inputStream.Read(buffer, 0, buffer.Length)) > 0)
  {
       HttpContext.Current.Response.OutputStream.Write(buffer, 0, read);
       HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
  }
  inputStream.Close();
  HttpContext.Current.Response.OutputStream.Close();
}

and jquery :

function GetDlLink(brandName, model, os, file) {
       $.ajax({
           type: 'POST',
           url: 'WS/Drivers.asmx/GetDownload',
           data: JSON.stringify(
               {
                   brandName: brandName,
                   modelName: model,
                   osName: os,
                   file: file
               }),
           cache: false,
           contentType: 'application/json; charset=utf-8',
           processData: false,
           responseType: 'blob',
           // ******* this is important for getting binary response types *******************
           xhrFields: {
               responseType: 'blob'
           },
           //==================================================================

           // function for openning browser download dialog
           success: function(data) {
               var blob = new Blob([data]);
               var link = document.createElement('a');
               link.href = window.URL.createObjectURL(blob);
               link.download = file;
               link.click();
           },
           error: function(xhr, ajaxOptions, thrownError) {
              alert("error");

           }
       });
   }

but nothing is happening in browser!!!

Upvotes: 1

Views: 1166

Answers (2)

Dr developer
Dr developer

Reputation: 81

I know sending username and password of ftp site to client browser is not secure approach so used approach #2 but nothing happen on client browser. i found this trick to by pass this step: i enabled web service protocols Httpget and Httppost in my Web.config file under the <system.web> section

<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>

after that i remove Ajax call and JavaScript "GetDlLink" function. instead of that i create link of download from Webservice and pass parameters to it with URL like this:

<a href="WS/Drivers.asmx/GetDownload?brandName=brand&modelName=model&osName=os&file=file" >download file</a>

therefore do not change any code in "GetDownload" web method.

my client click on download file and "GetDownload" method connect to FTP server and check credentials and get back specified file stream. after that browser get the file and open download dialog gracefully.

Upvotes: 0

Martin Prikryl
Martin Prikryl

Reputation: 202292

You should not use ftp:// URLs for two reasons, at least:

  • You reveal the credentials (as you know) and there is not way to hide them;
  • All major web browsers are gradually removing a support for FTP.

Instead, you have to point the download link back to a script on your website. And have the script download the file from FTP and stream it back to the web browser.

For some examples of implementing such script in ASP.NET, see these questions:

Upvotes: 1

Related Questions