A class of file downloads

Source: Internet
Author: User
Tags bool datetime file url tostring
Download///<summary>
Download files
</summary>
public class Bddownloadfile
{
private string Url; The file URL address to download
private string Savepath; The directory of the files to be saved
private string errmsg; Save error message
private string Regvalue = @ "http://" ([\w-]+\.) +[\w-]+ (/[\w-/?%&=]*)? ";
private string SaveFile; Generated files

Public Bddownloadfile (String url,string path)
{
url = URL;
Savepath = path;
This. SaveFile = GetFileName ();
}

<summary>
return error message
</summary>
public string errormessage
{
Get
{
return this.errmsg;
}
}

public string Getsavefile
{
Get
{
return this. SaveFile;
}
}
public bool DownLoadFile ()
{
bool result = true;
if (! Checkurl (this. URL))
{
this.errmsg = "URL is illegal! ";
return false;
}
WebClient OBJWC = new WebClient ();
Objwc.credentials = CredentialCache.DefaultCredentials;
Try
{
byte[] Tmpdata = Objwc.downloaddata (this. URL);
if (Tmpdata.length > 0)
{
FileStream OBJFS = new FileStream (this. Savefile,filemode.create);
Objfs.write (tmpdata,0, (int) tmpdata.length); Writing data to a file
Objfs.close ();
This.errmsg = "There is data!" ";
}
Else
{
result = false;
This.errmsg = "No data received!" ";
}
}
catch (System.Net.WebException e)
{
This.errmsg + = "<li> Download data error occurred!" "+ e.message;
return false;
}
catch (System.uriformatexception e)
{
This.errmsg + = "<li> access URL is invalid! "+ e.message;
return false;
}
catch (Exception e)
{
this.errmsg = "error message: &LT;LI&GT;." +e.message;
result = false;
}
Finally
{
Objwc.dispose ();
}
return result;
}

<summary>
Check if the URL is legal
</summary>
<param name= "Chkurl" > to check the URL </param>
<returns></returns>
private bool Checkurl (string chkurl)
{
Regex reg = new regex (regvalue);
Match match = Reg. Match (Chkurl);
Return match. Success;
}

<summary>
To obtain a file URL, if there are files, then processing, if not, then return. html
</summary>
<param name= "Chkurl" ></param>
<returns></returns>
private string Getfiletype (String chkurl)
{
if (Chkurl.lastindexof ("/") = = (chkurl.length-1)//No specific file
return "HTML";
int j = 0;
for (int i = 0; i < chkurl.length; i++)
{
if (Chkurl.indexof ("/", I) >-1)
{
i = Chkurl.indexof ("/", I);
j + +;
}
}
if (J < 3)
return "HTML";
Get the character after "/" and then draw the file type
String end = Chkurl.substring (Chkurl.lastindexof (".") +1);
Switch (end)
{
Case "ASP":
Case "aspx":
Case "JSP":
Case "PHP":
return "HTML";
Default
return end;
}
}

private String GetFileName ()
{
String fileName = this. Savepath + "\" + System.DateTime.Now.Year.ToString () +system.datetime.now.month.tostring () + System.DateTime.Now.Day.ToString () +system.datetime.now.minute.tostring () +system.datetime.now.second.tostring () +common.makerandom (4). ToString () + "." +getfiletype (this. URL);
for (; File.exists (fileName);)
{
FileName = this. Savepath + "\" + System.DateTime.Now.Year.ToString () +system.datetime.now.month.tostring () + System.DateTime.Now.Day.ToString () +system.datetime.now.minute.tostring () +system.datetime.now.second.tostring () +common.makerandom (4). ToString () + "\" +getfiletype (this. URL);
}
return fileName;
}
}//bddownloadfile End



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.