. Net generic File Upload class (large files can be uploaded and 480 MB has been tried locally)

Source: Internet
Author: User

. ASPX page

<Asp: Image id = "imglogo" runat = "server"/> <br/> <asp: label id = "labmsg" runat = "server" forecolor = "# cc0000"> </ASP: Label> <br/> <asp: fileupload id = "uploadlogo" runat = "server" width = "60%" Height = "22px"/> <asp: button id = "btnupload" runat = "server" text = "Upload" onclick = "btnupload_click"/>

. Aspx. CS page 

// Upload button protected void btnupload_click (Object sender, eventargs e) {uploadfile uploadfileobj = new uploadfile (); // instantiate the object upload class uploadfileobj. maxfilesize = 100; // sets the maximum length of the uploaded file, in the unit of K uploadfileobj. filetype = "JPG | JPEG | GIF | PNG"; // you can specify the file type to be uploaded. String uploadpath = server. mappath ("~ /Uploadfiles/other/"); // sets the full upload directory path uploadfileobj. uploadfilego (uploadpath, uploadlogo); // File Upload labmsg. TEXT = uploadfileobj. uploadinfo; // If (uploadfileobj. uploadstate = true) {imglogo. imageurl = "~ /Uploadfiles/other/"+ uploadfileobj. newfilename; // display image }}

Uploadfile. CS File Upload class

Using system; using system. data; 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; namespace common {// <summary> File Upload class </Summary> public class uploadfile {# region field private string _ uploadinfo; // return information of file upload. Private bool _ uploadstate; // The returned status of file upload. Private string _ filetype; // The type of the file to be uploaded. Private int _ filesize; // the size of the uploaded file, in the unit of B private int _ maxfilesize; // the maximum length of the uploaded file, in the unit of B private string _ newfilename; // name of the uploaded file. # Endregion // <summary> initial File Upload class (default) </Summary> Public uploadfile () {_ uploadinfo = "NONE"; _ uploadstate = false; _ filetype = "*"; _ maxfilesize = 1024000; // 1000 K is 1024 * 1000b, in the unit of B _ newfilename = "";} # region attributes // <summary> returned information for file upload </Summary> Public String uploadinfo {set {_ uploadinfo = value ;}get {return _ uploadinfo ;}} /// <summary> the returned status of the object upload. The value true indicates that the object is successfully uploaded. The value false indicates that the object fails to be uploaded. </Summary> Public bool uploadstate {Set {_ uploadstate = value;} get {return _ uploadstate; }}// <summary> specifies the type of the file to be uploaded. * by default, it indicates any type or custom type, for example, "JPG | GIF | BMP" </Summary> Public String filetype {set {_ filetype = value;} get {return _ filetype ;}} /// <summary> size of the uploaded file, in the unit of K </Summary> Public int filesize {get {return _ filesize/1024 ;}} /// <summary> Maximum length of the size of the uploaded file, in the unit of K </Summary> Public int maxfilesize {set {_ maxfilesize = value * 1024;} Get {return _ maxfilesize/1024 ;}/// <summary> uploaded file name </Summary> Public String newfilename {set {_ newfilename = value ;} get {return _ newfilename ;}} # endregion # region upload main program // <summary> upload local files to the server </Summary> // <Param name = "strsavedir"> physical path saved on the server. </Param> // <Param name = "fileuploadctrlid"> specifies the object to be uploaded. The fileupload control is used here, </param> // <param> If the second parameter is an HTML input (File) control, you can change it: htmlinputfile htmctrlobjuploadfile </param> // <returns> </returns> Public void uploadfilego (string strsavedir, fileupload fileuploadctrlid) {int intfileextpoint = fileuploadctrlid. postedfile. filename. lastindexof (". "); // the last one. string strfileextname = fileuploadctrlid. posted File. filename. substring (intfileextpoint + 1). tolower (); // get the suffix of the uploaded file. _ Filesize = fileuploadctrlid. postedfile. contentlength; // the size of the uploaded file (byte if (_ filesize = 0) // determine whether a file needs to be uploaded or whether the selected file is 0 bytes. {_ Uploadinfo = "the file to be uploaded is not selected or the selected file size is 0 bytes"; _ uploadstate = false; return; // The File Upload Status and information are returned.} If (_ filesize> _ maxfilesize) // limit the size of the file to be uploaded (byte ). {_ Uploadinfo = "the size of the uploaded file exceeds the limit (" + (_ maxfilesize/1024 ). tostring () + "k)"; _ uploadstate = false; return; // returns the File Upload Status and information.} If (_ filetype! = "*") {If (_ filetype. tolower (). indexof (strfileextname. tolower (). trim () =-1) // determines whether the file type to be uploaded is within the permitted range. {_ Uploadinfo = "file type that cannot be uploaded (allowed type: |" + _ filetype + ")"; _ uploadstate = false; return; // returned File Upload Status and information} If (_ newfilename = "") {datetime dtenow = datetime. now; // defines the date object, which is the name of the uploaded file. _ Newfilename = dtenow. year. tostring () + dtenow. month. tostring () + dtenow. day. tostring () + getrandomstr (8); // randomly name the uploaded file, date + random number. _ Newfilename = _ newfilename + ". "+ strfileextname; // file name containing the extension} fileuploadctrlid. postedfile. saveas (this. getsavedirectory (strsavedir) + _ newfilename); // Save the uploaded file to the specified physical path with a new file name. _ Uploadinfo = "File Uploaded successfully"; // return the physical path of the uploaded Server File. _ Uploadstate = true ;}# endregion /// <summary> obtain the random number of digits </Summary> /// <Param name = "rndnumcount"> the number of digits of the random number. </Param> // <returns> </returns> private string getrandomstr (INT rndnumcount) {string randomstr; randomstr = ""; random RND = new random (); for (INT I = 1; I <= rndnumcount; I ++) {randomstr + = RND. next (0, 9 ). tostring ();} return randomstr ;} /// <summary> obtain the physical path of the uploaded file storage directory </Summary> /// <Param name = "directorypath">. </Param> /// <returns> returns the directory where the file is stored. </Returns> Public String getsavedirectory (string directorypath) {If (! Directory. exists (directorypath) // checks whether the current directory exists. {Directory. createdirectory (directorypath); // create a directory for storing uploaded files.} Return directorypath ;}# region attachment: Modify the upload size configuration/* in c: \ windows \ Microsoft. in the net \ framework \ v1.1.4322 \ config directory, find the file maxrequestlength = "4096" and increase the value. For example: 102400 the unit of this parameter should be kb. The above method is to modify the global. If you need to modify a project, it is to modify the web in the project. in the <system. web> </system. web>, 

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.