C # Implement the upload of the web file-1
Using system; using system.data.sqlclient; using system.web.ui.htmlcontrols; using system.drawing.imaging; using system.configuration; using system.drawing;
Namespace Zhuanti {///
Public static string [] UPLOADFILE (htmlinputfile file, string upload_dir) // Realize player files
Master function {string [] arr = new string [5]; string filename = getUniqueString (); // Get a non-duplicate file name string fileorginname = file.postedfile.FileName.Substring
(file.postedfile.filename.lastIndexof ("//") 1); // Get the original name IF of the file (file.postedfile.contentLength <= 0) {Return Null;} String PostFileName; string filepath = upload_dir.tostring (); String path = filepath "//";
Try {int pos = file.postedfile.filename.lastIndexof (".") 1; postfileName = file.postedfile.filename.substring (pos, file.postedfile.filename.length-pos); file.postedfile.saveas (PATH FileName "." Postfilename); // Store the specified file to the specified directory} catch (exception exec) {throw (exec);
Double unit = 1024; double size = math.round (file.postedfile.contentLENGTH / Unit, 2); arr [(int) file.file_size] = size.toString (); // File size Arr [(int) file. FILE_POSTNAME] = postFileName; // File type (file suffix name) Arr [(int) file.file_sysname] = filename; // file system name arr [(int) file.file_orginname] = fileorginname; // File original name Arr [(int) file.file_path] = path filename "." postfilename; // file path Return Arr;}
Public Static Bool OperateDB (String Sqlstr) // Create an associated {IF (Sqlstr == String.empty) Return False; SqlConnection MyConnection = New SqlConnection (ConfigurationSettings.Appsetting
["connString"]); SQLCommand mycommand = new sqlcommand (sqlstr, myconnection);
MyConnection.open (); mycommand.executenonquery (); myconnection.close ();
Return True;}
Public static string getUniqueString () // Get a non-repetitive file name {const Int random_max_value = 1000; String Strtemp, Stryear, Strmonth, Strday, Strhour, Strminute, Strsecond, StrmilliseCond;
DateTime DT = DATETIME.NOW; int rnumber = rnd.next (random_max_value); stryear = dt.year.tostring (); strMonth = (DT.MONTH> 9)? Dt.month.tostring (): "0" DT .MONTH.TOSTRING (); strDay = (dt.day> 9)? Dt.day.tostring (): "0" dt.day.tostring (); strhur = (dt.Hour> 9)? Dt. Hour .Tostring (): "0" dt.Hour.toString (); strMinute = (DT.minute> 9)? Dt.minute.toT7tring (): "0" dt.minute.toString (); strsecond = Dt.second> 9)? dt.second.toString (): "0" Dt.second.toString (); strmillisecond = dt.millisecond.toString ();
Strtemp = streyear strMonth strDay "_" strhur strMinute strSecond "_"
StrmilliseCond "_" rNDNumber.toString ();
Return strtemp;
}
}
C # Implement the upload of the web file-2
In web programming, we often need to upload some local files to the web server. After upload, users can easily browse this through browsers.
Some documents, the application is very widespread.
So use C # how to implement file upload function? The following writer briefly introduces it.
First, add a Uploaded Web Form in your Visual C # Web Project, in order to upload files, you need to select in Toolbox
Select the File Field control of the HTML class, join this control into the web form, however, this control is not a server control, we need to
It plus the following code: so it becomes a server control, and if we need to upload files simultaneously, we can add this control accordingly.
It should be noted that the code must be set to the