使用 ASP.NET 下载文件

3

每晚我都需要使用asp.net将数据文件和一些图像文件下载到本地服务器。有什么最好的方法可以做到这一点?

更新

好的,在查看了回复后,我意识到我最初使用asp.net的想法并不是一个好选择。如果要用C#编写控制台应用程序,你会怎么写呢?我不确定我正在使用哪些类来连接并从远程服务器下载文件。

谢谢

6个回答

9

如何在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");
            }
        }
    }
}

1
要从远程URL下载任何文件,您可以在System.Net命名空间内使用C#中的WebClient。
公共FileResult Song(string 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/ 可以帮助您安排任务。


0

如果您想每晚执行此操作,可以在服务器上设置定时任务,让其访问特定的ASP.NET网页以执行所需代码。

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


如果你要投反对票,至少给出一个合理的理由。如果他说他必须使用ASP.NET来完成它,你可以提供通过ASP.NET完成它的解释,而不是告诉他他完全做错了。 - TheTXI
糟糕的解决方案。这个路由逻辑不必要地通过两个不同的范例。网站/应用程序不应该用于管理服务逻辑。如果您只需要服务,请编写一个服务或控制台应用程序。 - doekman
抱歉,TheTXI,但如果一个程序员问如何使用错误的范例来获得所需的结果,我会建议他使用更好、更清晰的范例。你不应该为了符合程序员熟悉的上下文而将错误的解决方案适应于问题。 - Kevin
TheTXI,如果你要建议像定时任务“触发”一个ASP.NET页面并执行其代码这样的事情,你最好提供一个更好的解释,说明它将如何发生。否则,我不会感到惊讶,如果这个建议很快就被投票否决。 - Kon

0

通常情况下,您不会通过ASP.NET来执行此操作。网页的代码仅在发出HTTP/HTTPS请求时执行,而这通常是由使用Web浏览器或Web爬虫的最终用户触发的。

您需要使用类似FTP的东西来下载文件,并使用Windows服务自动启动下载。


根据问题更新进行了更新:

你可以通过 谷歌搜索 很容易地找到有关在 .NET 中通过FTP下载的大量信息。

看看这个 C# FTP客户端库

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

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

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


0

在System.Net.WebClient类上使用DownloadFile方法可能是一个不错的起点。 您只需提供URL字符串和文件名,它就会完成剩下的工作。 MSDN上的System.NET.WebClient

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


0
如果您无法使用FTP完成任务,可以使用HttpWebRequest和HttpWebResponse来完成。查看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>

*/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接