C#WebClient-从ftp下载文件到客户端 [英] C# WebClient - downloading a file from an ftp to client

查看:386
本文介绍了C#WebClient-从ftp下载文件到客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将授权代码从表单传递给控制器​​,评估该代码,如果匹配,请呼叫ftp站点以触发下载事件,用户会在该事件中接收文件.

I'm looking to pass in an authorization code from a form to a controller, evaluate the code and if it matches, call an ftp site to trigger a download event where the user receives a file.

我有两个限制,1.我需要传递ftp站点凭据,2.我需要向用户隐藏url.在ftp站点上以用户"身份登录后,我可以将URL粘贴到浏览器中,并触发zip文件的自动下载事件.我想我必须使用下面的方法来传递凭据,但是,我想避免必须先将文件下载到我的服务器上,然后再将其传递给用户. (是对处理时间的两倍吗?)我需要一种将凭据传递到ftp站点并触发下载事件以直接将用户计算机转到通常的下载"文件夹的方法...

I have two constraints, 1. I need to pass the ftp site credentials, 2. I need to hide the url from the user. Once logged in, 'as a user' on the ftp site, I can paste the url into the browser and it triggers an auto download event of the zip file. I think I have to use the below approach to pass off the credentials, however, I want to avoid having to first download the file onto my server to then turn around and pass it to the user. (double the processing time right?) I need a way to hand off the credentials to the ftp site and trigger the download event to go straight to the users machine to the usual 'download' folder...

我不知道如何执行此操作,也找不到任何可靠的参考.大多数文件都显示了如何从服务器向用户处理该文件,因为该文件存储在应用程序所在的位置.任何参考或样品表示赞赏!

I have no clue on how to do this and cant find any solid references. Most are showing how to process it to the user from the server because the file is stored where the application lives. Any references or samples is appreciated!

提前谢谢!

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult AuthCode(string code)
    {
        var username = "someuser";
        var password = "somepass";

        if(code.Equals("$%^123XYZ"))
        {
            var url = "ftp://url.zip";
            var fileName = "Some Name";

            var client = new WebClient();
            client.Credentials = new NetworkCredential(username, password);
            client.DownloadFile(url, fileName);

            return View("GoodCode");
        }
        else
        {
         return View("BadCode");
        }
    }

推荐答案

此处是从以下位置下载文件的示例使用流的FTP.

FtpWebRequest class supports stream in response. Here is an example of file download from FTP using stream.

要在操作FileStreamResult中返回流,可以使用将流传递到响应中

To return a stream in the action FileStreamResult can be used pass the stream into the response

return new FileStreamResult(stream, "application/pdf")
 {
     result.FileDownloadName = "somefile.pdf";
 };

这篇关于C#WebClient-从ftp下载文件到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆