Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

html - 如何将FTP文件发送到客户端浏览器?(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.

(我编写了asp.net Web服务以生成ftp下载链接并将其发送到客户端浏览器,以便客户端可以下载它。)

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.

(我通过Ajax响应链接发送了该链接。)

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

(所以如何防止用户查看我的ftp用户名和密码?)

or how to encrypt link?

(或如何加密链接?)

approach #2:

(方法2:)

to avoid above link i wrote code to send chunk of data but nothing is happening in browser.

(为了避免上述链接,我编写了发送数据块的代码,但是浏览器中没有任何反应。)

(I am using fluentftp library . )

((我正在使用fluentftp库。))

[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 :

(和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!!!

(但是在浏览器中什么也没发生!!!)

  ask by Dr developer translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

(出于以下两个原因,您不应使用ftp:// URL:)

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

    (所有主要的Web浏览器都逐渐取消对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.

(并让脚本从FTP下载文件并将其流回Web浏览器。)

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

(有关在ASP.NET中实现此类脚本的一些示例,请参见以下问题:)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...