Reputation: 4868
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
Upvotes: 3
Views: 6304
Reputation: 21
To download any file from remote URL you can use WebClient in C# inside System.Net namespace.
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");
}
For downloading data every night you could use task scheduler. http://www.quartz-scheduler.net/ can help you for scheduling tasks
Upvotes: 0
Reputation: 3168
"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");
}
}
}
}
Upvotes: 9
Reputation: 5220
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>
*/
Upvotes: 0
Reputation: 612
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.
Upvotes: 0
Reputation: 27431
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
Upvotes: 0
Reputation: 37875
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.
Upvotes: 0