views:

1055

answers:

5

Hello all,

There is a data file and some image files that I have to download to our local servers every night using asp.net. What is the best way to do this?

UPDATE

Ok, after viewing the responses I see my initial post of using asp.net is a poor choice. How would you write it for a console app in C#. I am not sure what classes I am using to connect and pull down files from the remote server.

Thanks

A: 

To do it every night you would set it up as a scheduled task on your server to hit a particular asp.net web page to execute the code you want.

Your ASP.NET page would have the code for going out and downloading the file and doing whatever processing is required.

TheTXI
if you are going to vote down at least give a logical reason. If he says he has to do it using ASP.NET you can provide the explanation of how to do it via ASP.NET, not telling him that he is just flat out doing it wrong.
TheTXI
Bad solution. This routes logic needlessly through two separate paradigms. Web sites/apps should not be used to manage service logic. If you have a service-only requirement, write a service or console app.
Sorry, TheTXI, but if a programmer asks how to use the wrong paradigm to get what he needs, I'm going to suggest that he use a better, cleaner paradigm instead. You shouldn't fit a bad solution to a problem, just to stay within the context of what a programmer is comfortable with.
TheTXI, if you're going to suggest something like having a scheduled task "hit" an ASP.NET page and execute its code, you better provide a better explanation on how that's going to happen. Otherwise, I wouldn't be surprised if this was voted down quicky.
Kon
A: 

You typically would not do this via ASP.NET. A web page's code is executed only when an HTTP/HTTPS request is made and that is usually triggered by end-users with a web browser or web crawlers.

You want to use something like FTP to download files and a Windows service to automate the download initiation.


UPDATED in response to question update:

You can do an easy google search to find lots of information on downloading via FTP in .NET.

Check out this C# FTP Client Library.

And here are some nice links on creating a Windows service in .NET:

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

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

Kon
A: 

The DownloadFile Method on the System.Net.WebClient Class is probably a good place to start. You give it a URL string and a Filename and it does the rest. System.NET.WebClient at MSDN

You can even set custom user-agent strings and upload files using this class.

tooleb
A: 

If you can't do it with FTP you want to use HttpWebRequest and HttpWebResponse to do the job. 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>

*/
thaBadDawg
+7  A: 

"How would you write it for a console app in C#."

Create a C# console application. Add a reference to 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");
      }
     }
    }
}