Search code examples
c#asp.netfluentftp

send file stream through http not working


I am using fluentftp library .

I have 2 server separately ,FTPserver and WebServer. so my scenario is to download large file from ftp server and send it to my client through http ajax response. i created a webservice .asmx file and a webmethod to create downloading data.

This is my code to send chunk of data but nothing is happening in browser.

[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();
}

Solution

  • 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>
    

    and then 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.