Asp.net uploads files to folders using fileupload

Source: Internet
Author: User

Using system;
Using system. Data;
Using system. configuration;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. LINQ;
Using system. IO;
Public class upfield: system. Web. UI. Page
{
Public upfield ()
{
}
Public void upfile (fileupload fiup2)
{
String savepath = (system. web. httpcontext. current. server. mappath (".. \\.. \ filesave "+" \ "+ session [" ID "]);
String [] AllowExtensions = {". doc ",". docx ",". pptx ",". ppt ",". xls ",". XLSX ",". jpg ",". JPEG ",". GIF ",". PNG "," pdf ",". RAR ",". zip ",". 7z ",". TXT "};
String fileextensi = system. Io. Path. getextension (fiup2.filename). tolower ();
Int A = 0;
For (INT I = 0; I <AllowExtensions. length; I ++)
{
If (fileextensi = AllowExtensions [I])
A = 1;
}
If (! Directory. exists (savepath ))
{
Directory. createdirectory (savepath );
}
Else
{
If (fiup2.hasfile)
{
If (! = 0)
{
If (fiup2.postedfile. contentlength <10485760)
{
Try
{
String filename = fiup2.filename;
Httppostedfile postfile = fiup2.postedfile;
String dfilename = path. getfilename (fiup2.filename). Replace (path. getextension (fiup2.filename), ""). Replace ("+ ","");
String lfiletype = filename. substring (filename. lastindexof (".") + 1 );
String time1 = datetime. now. tostring (). replace ("/",""). replace (":",""). replace ("-",""). replace ("","");
String lfilename = dfilename + time1 + "." + lfiletype;
Fiup2.saveas (system. web. httpcontext. current. server. mappath (".. \\.. \ filesave "+" \ "+ session [" ID "] +" \ "+ lfilename ));
String filetype = fiup2.postedfile. contenttype;
Int filesize = fiup2.postedfile. contentlength;
String filemappath = ".. \\.. \ filesave "+" \ "+ session [" ID "] +" \ "+ fiup2.filename; // you can modify the path of the uploaded folder.
String filetime = datetime. Now. tostring ();

/* Static. setsuzu (filename, lfilename, filetype, filesize, filemappath, static. I); // write data to the pre-stored Array
Static. I ++; // I ++; */mark The Multifile upload.
}
Catch (exception ex)
{
Throw new exception ("Upload error! ");
}
}
Else
{
Throw new exception ("please upload a file smaller than 10 MB ");
}
}
Else
{
Throw new exception ("please upload the appropriate file type ");
}
}
Else
{
Throw new exception ("select the file to upload! ");
}
}
}

}

This is an encapsulation class for file upload. You can call this class as needed. The call method is as follows:

Protected void btfadd_click (Object sender, eventargs e) // upload
{
Try
{
Upfield up = new upfield ();
Up. upfile (fiup2 );
}
Catch (exception ex)
{
Response. Write ("<script language = 'javascript '> alert ('" + ex. Message + "') </SCRIPT> ");
}
}

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.