A simple upload class

zhaozj2021-02-16  95

Also, many projects require uploading, and every time you have to write up the code, it is more troublesome. I want to write a class to complete the uploaded task to reduce the burden in the development. It is very simple written, but it is practical ^ _ ^

Using system.web; using system.text;

Namespace upfile {///

/// UPFILE class complete file upload and deletion function ///

public class MyUpload {private System.Web.HttpPostedFile postedFile = null; private string savePath = ""; private string extension = ""; private int fileLength = 0; // upload control public System.Web.HttpPostedFile PostedFile {get {return postedFile } Set {postedfile = value;}}

// Storage path public string savePath {get {if (savePath! = ") Return SavePath; Return" C: // ";} set {savepath = value;}}

// File size public int filelength {get {ix! = 0) {Return FileLength;} Return 1024;} set {fileLength = value * 1024;}}

// File format public string extension {get {i (extension! = "") {Return extension;} return ";} set {extension = value;}}

Public string path {int pos = path.lastindexof ("//"); return path.substring (POS 1);}

// Upload file public string Upload () {if (postedfile! = Null) {Try {string filename = pathtoname (postedfile.filename); if (! Filename.endswith (extension)) {return "You must choose" EXTENSION " This format file! ";} If (postedfile.contentLength> filelength) {return" file too big! ";} If (file.exists ((savepath //" filename)))) file name repetition! "} Else {postedfile.saveas (SavePath " // " filename); Return" Upload file success! ";}} Catch (system.exception eXC) {return eXc.Message;}} return" Please select the file upload! ";} // Display file name public string UpFileName () {string filename = pathtoname (postedfile.filename); Return FileName;

// Display detailed path PUBLIC STRING URL () {string filename = pathtoname (posted file.filename); string urls = savepath "//" filename; return urls;}

/ / Delete uploaded file public string delete (String URL) {file.delete (URL); Return "Delete file success!";} Catch (system.exception eXC) {return exc.Message;}}

}

How to use: 1. Add UPFILE.DLL in the reference 2.using upfile; 3.private myupload myupfile; 4.myupfile = new myupload (); // instantiate myupfile.postedfile = this.file1.postedfile; // Set up the upload control Myupfile.savePath = Server.mAppath ("/); // Set up the upload path 5. Upload button: this.label2.text = myupfile.upload (); // Upload IF (this.label2.text == " Upload file success! ") {This.upfile.text = myupfile.upfilename (); // Display file name this.URL.TEXT = myupfile.URL (); // Display file path} 6. Remove button: this.label2 .Text = myupfile.delete (this.URL.Text); if (this.label2.text == "delete file success!") {This.upfile.text = ""; // Display file name this.url.Text = ""; // Display file path} aspx: (

)

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

New Post(0)