How to batch download images through Asp.net

Source: Internet
Author: User

Sometimes, we can see beautiful image wallpapers, landscape pictures, etc. You may need to download images from a fixed website. It takes a lot of time to open a webpage and save it as our file name. Is there a way to batch download? Yes! The premise is that the image path and file name are regular.

Of course, you can use the batch download feature of Internet Express or thunder, which is not discussed in this article.

The downloadfile method of WebClient is used here. Because the code is relatively simple, I will not talk about it much.

// Batchdownload. aspx
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "batchdownload. aspx. cs" inherits = "_ default" DEBUG = "true" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> batch image download page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: Label id = "label1" runat = "server" text = "label"> </ASP: Label>
</Div>
</Form>
</Body>
</Html>

// Batchdownload. asp. CS

Using system;
Using system. Data;
Using system. drawing;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. IO;
Using system. net;
Using system. text;

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Stringbuilder sb = new stringbuilder ();
Int m = 0;
For (INT I = 1; I <200; I ++)
{
For (Int J = 1; j <100; j ++)
{
String folder;
If (I <100)
{
Folder = I. tostring ("000 ");
}
Else
{
Folder = I. tostring ("0000 ");
}
String uristring = "http://www.photodownloadsite.com/200702/" + folder + @ "/" + J. tostring () + @ ". jpg ";
Uri address = new uri (uristring );
String filename = "A" + I. tostring ("000") + M. tostring ("00000") + ". jpg ";

WebClient WC = new WebClient ();
Try
{
// Bitmap BMP = new Bitmap (WC. openread (Address ));
WC. downloadfile (address, server. mappath (filename ));
}
Catch (exception exc)
{
}
Finally
{
If (WC! = NULL) WC. Dispose ();
}
// Sb. append (uristring + "<br/>" + server. mappath (filename) + "<br/> ");
// Sb. append (" <br/> ");

M ++;
}
}
// Label1.text = sb. tostring ();
}
}

Here is a question:
What should I do if I want to download an image whose height and width are greater than a specified size? Think about it. In fact, the above sentence roughly shows the related practices: // bitmap BMP = new Bitmap (WC. openread (Address ));

I would like to extend it here:
What should I do if I need to obtain a similar stock price chart in real time?

When I get this picture, the link to this picture is: http://image2.sinajs.cn/newchart/min/n/sh600030.gif? 1209560972156
Note that a question mark (?) is added after the image file name (?), Then there is a string of numbers: 1209560972156. Why? The reason is to prevent the browser from caching. In fact, you can use any string of characters to replace it! The next work is relatively simple. Download it and it becomes what you need. OK, just the train of thought. The other is up to you. Pleasant programming! :)

Note the following:
This article describes the programming method from a technical perspective only. You must use online resources reasonably and legally. Do not abuse or infringe on the interests of others.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.