【问题标题】:Download files using asp.net使用 asp.net 下载文件
【发布时间】:2009-01-19 14:59:39
【问题描述】:

有一个数据文件和一些图像文件,我必须每晚使用 asp.net 下载到我们的本地服务器。最好的方法是什么?

更新

好的,在查看回复后,我发现我最初使用 asp.net 的帖子是一个糟糕的选择。您将如何为 C# 中的控制台应用程序编写它。我不确定我使用什么类来连接远程服务器并从远程服务器下载文件。

谢谢

【问题讨论】:

    标签: c# .net asp.net file download


    【解决方案1】:

    “你将如何用 C# 为控制台应用程序编写它。”

    创建一个 C# 控制台应用程序。添加对 System.Net 的引用。

    
    using System;
    using System.Net;
    
    namespace Downloader
    {
        class Program
        {
            public static void Main(string[] args)
            {
                using (WebClient wc = new WebClient())
                {
                    wc.DownloadFile("http://www.mydomain.com/resource.img", "c:\\savedImage.img");
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      要从远程 URL 下载任何文件,您可以在 System.Net 命名空间内的 C# 中使用 WebClient。

      public FileResult Song(字符串歌曲) {

              WebClient client = new WebClient();
              var stream = client.OpenRead("http://www.jplayer.org/audio/mp3/Miaow-03-Lentement.mp3");
      
              return File(stream, "audio/mpeg");
      
          }
      

      对于每晚下载数据,您可以使用任务调度程序。 http://www.quartz-scheduler.net/可以帮你安排任务

      【讨论】:

        【解决方案3】:

        要每天晚上执行此操作,您可以将其设置为服务器上的计划任务,以访问特定的 asp.net 网页以执行您想要的代码。

        您的 ASP.NET 页面将包含用于下载文件和执行所需处理的代码。

        【讨论】:

        • 如果你要投反对票,至少要给出一个合乎逻辑的理由。如果他说他必须使用 ASP.NET 来做,你可以提供如何通过 ASP.NET 来做的解释,而不是告诉他他完全做错了。
        • 错误的解决方案。这不必要地通过两个独立的范例来路由逻辑。不应使用网站/应用程序来管理服务逻辑。如果您有纯服务需求,请编写服务或控制台应用程序。
        • 对不起,TheTXI,但是如果程序员问如何使用错误的范例来获得他需要的东西,我会建议他使用更好、更清洁的范例。你不应该为一个问题找到一个糟糕的解决方案,而只是停留在程序员熟悉的范围内。
        • TheTXI,如果您要建议诸如让计划任务“命中” ASP.NET 页面并执行其代码之类的事情,您最好提供一个更好的解释来说明这将如何发生。否则,如果这很快被否决,我不会感到惊讶。
        【解决方案4】:

        您通常不会通过 ASP.NET 执行此操作。网页代码仅在发出 HTTP/HTTPS 请求时才会执行,并且通常由最终用户使用网络浏览器或网络爬虫触发。

        您想使用诸如 FTP 之类的东西来下载文件并使用 Windows 服务来自动启动下载。


        针对问题更新进行了更新:

        您可以通过简单的google search 查找大量有关通过 .NET 中的 FTP 下载的信息。

        看看这个C# FTP Client Library

        这里有一些关于在 .NET 中创建 Windows 服务的不错的链接:

        http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx

        http://www.developer.com/net/net/article.php/2173801

        【讨论】:

          【解决方案5】:

          System.Net.WebClient 类上的 DownloadFile 方法可能是一个不错的起点。 你给它一个 URL 字符串和一个文件名,剩下的就交给它了。 System.NET.WebClient at MSDN

          您甚至可以使用此类设置自定义用户代理字符串并上传文件。

          【讨论】:

            【解决方案6】:

            如果你不能用 FTP 来做,你想用 HttpWebRequest 和 HttpWebResponse 来做这个工作。 From MSDN:

            using System;
            using System.Net;
            using System.Text;
            using System.IO;
            
            
                public class Test
                {
                    // Specify the URL to receive the request.
                    public static void Main (string[] args)
                    {
                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create (args[0]);
            
                        // Set some reasonable limits on resources used by this request
                        request.MaximumAutomaticRedirections = 4;
                        request.MaximumResponseHeadersLength = 4;
                        // Set credentials to use for this request.
                        request.Credentials = CredentialCache.DefaultCredentials;
                        HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
            
                        Console.WriteLine ("Content length is {0}", response.ContentLength);
                        Console.WriteLine ("Content type is {0}", response.ContentType);
            
                        // Get the stream associated with the response.
                        Stream receiveStream = response.GetResponseStream ();
            
                        // Pipes the stream to a higher level stream reader with the required encoding format. 
                        StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);
            
                        Console.WriteLine ("Response stream received.");
                        Console.WriteLine (readStream.ReadToEnd ());
                        response.Close ();
                        readStream.Close ();
                    }
                }
            
            /*
            The output from this example will vary depending on the value passed into Main 
            but will be similar to the following:
            
            Content length is 1542
            Content type is text/html; charset=utf-8
            Response stream received.
            <html>
            ...
            </html>
            
            */
            

            【讨论】:

              猜你喜欢
              • 2014-05-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多