A class download of the file [original]

xiaoxiao2021-03-06  59

Disclaimer: Recently, many websites quote their own articles, actually omitted authors, please quote the author of this article to save the author's information. ///

/// download file /// Public class bddownloadfile {private string URL; // To download the file URL address private string savepath; // Directory for saved file prizter string errmsg; // Save Error message Private string regvalue = @ "http: // ([] /W-] /.) [W-] (/[w- ./?%&=]*)? "; private string savefile; // generated file

Public bddownloadfile (string url, string path) {url = url; savepath = path; this.savefile = getFileName ();

///

/// Return error message /// public string errorMessage {get {return this.errmsg;}}

Public string getsavefile {get {return this.savefile;}} public boxfile;}} PUBLIC BOOL Downloadfile () {bool result = true; if (! Checkurl (this.ur)) {this.errmsg = "URL is not legal!"; 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); // Write the data Objfs.close () / 向 文件 向 (); "} else {result = False; this.errmsg = "No data is received!";}} catch (system.net.Webexception e) {this.errmsg =

  • When downloading data! " E.MESSAGE; RETURN FALSE } Catch (System.URIFORMATEXCEPTION E) {this.errmsg = "
  • E.Message; Return False;} catch (Exception E) {this.errmsg =" error message:
  • . " E.MESSAGE; Result = false;} finally {objwc.dispose ();} return Result;} /// /// check the URL ration /// /// To check URL /// Private Bool Checkurl (String Chkurl) {regex reg = new regex (regvalue); match match = reg.match (chkurl); return match.success;}

    ///

    /// NOS has a file, if there is a file, then process, if not, return .html /// /// /// private string getfiletype (string chkurl) {if (chkurl.lastindexof ("/") == (chkurl.length-1)) // No specific file return "html"; int J = 0; for (int i = 0; i - 1) {i = chkurl.indexof ("/", i); J ;}} f (j <3) return "html"; // acquired "/" after the character, then draw file type string end = chkurl.substring (".") 1); switch (END) {CASE "ASP": Case "JSP": Case "PHP": Return "HTML"; default: returnvename ()} 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.makeraand (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.toTString () system.datetime.now.second.tostring () Common.makeraand (4) .tostring () "//" getfiletype (this.URL);} returna filename } // bddownloadfile End

  • 转载请注明原文地址:https://www.9cbs.com/read-114624.html

    New Post(0)