【问题标题】:Download file from FTP server in asp.net mvc [closed]从asp.net mvc中的FTP服务器下载文件[关闭]
【发布时间】:2018-07-08 04:28:13
【问题描述】:

我的 FTP 服务器上有一些文件。现在我想允许用户下载文件。 我怎样才能做到这一点?

我在互联网上搜索了很多,但找不到一些有用的。

注意:我使用的是 MVC5 和 angularjs

我试过这个:

var  filePath = "FTP_FILE_PATH";
        Response.AddHeader("content-disposition", "inline; filename=" + "new");
        return File(filePath, "audio/mp3");

【问题讨论】:

  • 没有免费的编码服务。
  • 如果您不想使用 FTP,为什么要将文件放在 FTP 服务器上? 下载文件 你是什么意思?您想提供一个 FTP 地址,还是希望您的 Web 服务器对文件进行 FTP 传输,然后提供下载链接或将它们直接流式传输到响应中?如果您只是将“文件”放到您的网络服务器上并提供指向它们的链接,那会更有意义。总的来说这个问题不是很清楚,缺乏努力
  • 所以你想从 FTP 服务器获取它们,然后将它们作为对用户对 MVC 应用程序的 HTTP 请求的响应传递给用户,对吗?首先从 FTP 服务器下载文件(确保你可以用谷歌搜索一些 C#),然后提供文件(或内容流,如果这更容易的话)从你的应用程序下载(你似乎有一些基础知识代码已经)。您不能只粘贴ftp:// 链接作为文件名,这没有任何意义。我猜也可能用户没有直接访问 FTP 服务器的权限

标签: c# asp.net-mvc asp.net-mvc-5


【解决方案1】:

请注意,File 方法的 filename 参数需要一个存储在网络服务器本地文件系统中的文件的路径。此处不能传递 FTP 地址。

相反,从 FTP 加载文件并从接收到的 FtpStream 提供它。

try {
    /* Create an FTP Request */
    var ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
    /* Log in to the FTP Server with the User Name and Password Provided */
    ftpRequest.Credentials = new NetworkCredential(user, pass);
    /* When in doubt, use these options */
    ftpRequest.UseBinary = true;
    ftpRequest.UsePassive = true;
    ftpRequest.KeepAlive = true;
    /* Specify the Type of FTP Request */
    ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
    /* Establish Return Communication with the FTP Server */
    var ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
    /* Get the FTP Server's Response Stream */
    var ftpStream = ftpResponse.GetResponseStream();

    // TODO: you might need to extract these settings from the FTP response
    const string contentType = "application/zip";
    const string fileNameDisplayedToUser = "FileName.zip"

    return File(ftpStream, contentType, fileNameDisplayedToUser);
}
catch (Exception ex) { 
    _logger.Error(ex); 
}

这个答案来自Display an Image from Ftp Server

【讨论】:

  • 如何从 FTP 下载多个 Zip 格式的文件。以上代码适用于单个文件。
猜你喜欢
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多