简体   繁体   English

通过http发送文件流不起作用

[英]send file stream through http not working

I am using fluentftp library .我正在使用fluentftp库。

I have 2 server separately ,FTPserver and WebServer.我分别有 2 个服务器,FTPserver 和 WebServer。 so my scenario is to download large file from ftp server and send it to my client through http ajax response.所以我的方案是从 ftp 服务器下载大文件并通过 http ajax 响应将其发送到我的客户端。 i created a webservice .asmx file and a webmethod to create downloading data.我创建了一个 webservice .asmx 文件和一个 webmethod 来创建下载数据。

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

I enabled web service protocols Httpget and Httppost in my Web.config file under the <system.web> section我在<system.web>部分下的 Web.config 文件中启用了 Web 服务协议 Httpget 和 Httppost

<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:然后从 Webservice 创建下载链接,并使用如下 URL 将参数传递给它:

<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.因此不要更改“GetDownload”网络方法中的任何代码。

my client click on download file and "GetDownload" method connect to FTP server and check credentials and get back specified file stream.我的客户端单击下载文件和“GetDownload”方法连接到 FTP 服务器并检查凭据并取回指定的文件流。 after that browser get the file and open download dialog gracefully.在该浏览器获取文件并优雅地打开下载对话框之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM