WebClient downloads images from the Internet

Source: Internet
Author: User

I tested it many times and found a lot of information on the Internet, but I didn't find a desired one, such as the configuration file and heads. all the add header files have been tried, but they are still not implemented. All errors are reported. Finally, we finally found a simple implementation method:

1. First download msxml2.dll

2. client code

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "MsXmlTest. aspx. cs" Inherits = "MyWeb. MsXmlTest" %>

<! 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> </title>
<Style type = "text/css">
# Txt1
{
Width: 466px;
Height: 51px;
}
# Txt2
{
Width: 464px;
Height: 51px;
}
</Style>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Input type = "text" id = "txt1" runat = "server"/> <br/>
<Input type = "text" id = "txt2" runat = "server"/> <br/>
<Asp: Button ID = "btn" runat = "server" Text = "MsXml test" onclick = "btn_Click"/>
</Div>
</Form>
</Body>
</Html>

2. Implementation Method

/// <Summary>
/// Download the file in the specified url path
/// </Summary>
/// <Param name = "url"> specify the url path </param>
/// <Param name = "filename"> path to be saved </param>
/// <Param name = "type"> Request Method (GET or POST) </param>
Private void Down (string url, string filename, string type)
{
XMLHTTP xmlhttp = new XMLHTTPClass ();
Xmlhttp. open (type, url, false, null, null );
Xmlhttp. send ("");
If (xmlhttp. readyState = 4)
{
If (xmlhttp. status = 200)
{
Byte [] mybyte = (byte []) xmlhttp. responseBody;
FileStream fs = new FileStream (filename, FileMode. Create, FileAccess. Write );
Fs. Write (mybyte, 0, mybyte. Length );
Fs. Close ();
}
}
}

3. Call

Protected void btn_Click (object sender, EventArgs e)
{
// Down ("http://list.image.baidu.com/t/image_category/galleryimg/wallpaper/human/han_gmn.jpg", "E: // B .jpg", "GET ");
Down(this.txt 1. Value, this.txt 2. Value, "GET ");
}

Related Article

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.