Download image given a url in C# using the System.Net.HttpWebRequest class
Tech-Today

Download image given a url in C# using the System.Net.HttpWebRequest class


Saves image url object into the local machine


///
/// Saves the list of url object into the output directory
///

/// list of url path
/// root directory that will be used for saving

internal static void PhotoDownloader(ArrayList aList, string outputDir)
{
Image imageStream = null;
DateTime dt = DateTime.Now;
String newDir = String.Format("{0:yyyy_mm_dd}", dt);
Directory.CreateDirectory(outputDir + newDir);
int ctr = 1;
foreach (String filename in aList)
{
try
{
System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(filename);
httpWebRequest.AllowWriteStreamBuffering = true;
httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
httpWebRequest.Timeout = 30000; //30 seconds
System.Net.WebResponse webResponse = httpWebRequest.GetResponse();
System.IO.Stream webStream = webResponse.GetResponseStream();
imageStream = Image.FromStream(webStream);
webResponse.Close();
webResponse.Close();
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
}
Console.WriteLine((ctr++) + "Saving..." + filename);

String newFile = filename.Substring(filename.LastIndexOf("/") + 1);
imageStream.Save(outputDir + newDir + "/" + newFile);
}
}


You may ask, how can I do the same in java? Here's how:
http://czetsuya-tech.blogspot.com/2010/03/download-url-file-into-desktop-for.html




- How To Upload A File In Rest Api
This is how I define the method: @POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) ActionStatus uploadFile(@MultipartForm FileUploadForm form); And then the implementation: public ActionStatus uploadFile(FileUploadForm form) { File file...

- How To Download A File From The Server Using Javaee
The following snippet will accept a filename in the server's directory, set it as the content of the FacesContext for the user to download. public String downloadXMLInvoice(String fileName) { File file = new File(getXmlInvoiceDir().getAbsolutePath()...

- How To Read And Write An Object In A Textfile In C#
Long time ago I've a requirement to read sms messages from device to be written in a text file. Before I learned to serialize an object, I was saving it in a file comma-delimited. That approach is working but sometimes it's a pain to keep track...

- Rss Feed Reader Implementation In C#
Included in this page: 1.) Rss feed model (which you can customized according to your needs) 2.) Rss feed reader My Objective is to read an rss feed of a news site like google and yahoo: For example we want to read the RSS feed of yahoo news in the Philippines:...

- Download A Url File Into Desktop. For Example, Download Image File (gif, Jpg) Using Java And Apache Commons Io Api
To make this code works you need: 1.) http://commons.apache.org/io/download_io.cgi 2.) make sure to configure your build path and include the apache commons io jar (I've used eclipse-java ide) 3.) the url that will be save are stored in the input/input.txt...



Tech-Today








.