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> ");
}
}