C # Implement the upload of a web file

xiaoxiao2021-03-04  86

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 {///

/// This is a functional module for the player's corresponding function to implement the corresponding functions used in the player's upload file function. /// public class fileupload {public enum file / / Define a person to store an array of players to upload file information {file_size, // size file_postname, // type (file complication name) file_sysname, // system name file_orginname, // The original name file_path // file path} private Static random rnd = new random (); // Get a random number

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

in the code:

If there is no this property, it cannot be uploaded.

Then add a Web Form class to the Button to add the following code to the following code in this Web Form.

// Upload the image of the picture DateTime now = DateTime.now; // Take the time to the DataTime class in the object now String strbaselocation = "d: // web // fc // pic //" This is a file The absolute directory IF (UploadFile1.PostedFile.contentLength! = 0) // determines if the selection dialog is selected for 0 {UPLOADFILE1.POSTEDFILE.SAVEAR.TOSTRING ()

UPLOADFILE1.POSTEDFILE.CONTENTLENGTH.TOSTEDFILE.CONTENTLENGTH.TOSTRING () ". jpg"); // Perform upload, and automatically naming the file according to the date and file size, make sure not repeating the label1.text = "picture 1 has been uploaded, the file name is " now.dayofyear.tostring ()

UPLOADFILE1.POSTEDFILE.CONTENTLENGTH.TOSTRING () "JPG"; navigator.insert (System.xml.treePosition.After, XMLNodetype.eMent, "PIC1", "", ""); Navigator.XSERT (System.xml .TreePosition.firstchild, XmlNodType.text, "PIC1", "" ""; Navigator.Value = now.dayofyear.tostring () UPLOADFILE1.POSTEDFILE.CONTENTLENGTH.TOSTRING ()

". JPG"; navigator.movetoparent ();

The above code is used for a system that uses XML files to store news information using XML files. The following sentence is written online information.

In the XML file. If you want to upload additional type files, you only need to change JPG to the corresponding type of suffix name, if you change to DOC, you can upload it.

Word file, you can browse uploaded Word files directly.

[Precautions] 1. Upload files can not be unlimited;

2. Pay attention to the security of IIS;

3. When making an installer with Visual Studio installation, please pay attention to the absolute path problem where the installer is located;

4. Pay attention to the reusal problem after the file is uploaded.

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

New Post(0)