使用asp.net下载文件

时间:2009-01-19 14:59:40

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

我必须每天晚上使用asp.net将数据文件和一些图像文件下载到本地服务器。这样做的最佳方式是什么?

更新

好的,在查看了回复之后,我看到我使用asp.net的初始帖子是一个糟糕的选择。你会如何为C#中的控制台应用程序编写它。我不确定我用什么类来连接和下载远程服务器上的文件。

由于

6 个答案:

答案 0 :(得分: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 :(得分:1)

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

public 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/可以帮助您安排任务

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

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

您希望使用类似FTP的内容下载文件和Windows服务来自动启动下载。


更新问题时更新:

您可以轻松google search查找有关通过FTP下载的大量信息。

查看此C# FTP Client Library

以下是在.NET中创建Windows服务的一些不错的链接:

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

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

答案 4 :(得分:0)

System.Net.WebClient类上的DownloadFile方法可能是一个很好的起点。 你给它一个URL字符串和一个文件名,它完成其余的工作。 System.NET.WebClient at MSDN

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

答案 5 :(得分:0)

如果你不能用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>

*/