Recently, the author is busy with a project, involving an uploading problem, of course, this problem is a relatively common problem, and many people have written a good article in this area. Here, I only write what I want to use system.Web.ui.htmlControls.htmlinputFile to achieve the features I want:
First, put a few controls on the interface for collecting information:
Protected system.web.ui.webcontrols.button button1; // upload button protected system.web.ui.webcontrols.label label1; // Used to display information protected system.web.ui.webcontrols.label label2; // The display information protected system.Web.ui.WebControls.Label label3 ;; // is used to display information protected system.web.ui.webcontrols.label label4 ;; // is used to display information protected system.web.ui.htmlControls. HTMLINPUTFILE FILE1; // Upload control
At the same time, since the IO operation is transmitted up, the following namespace is introduced:
Using system.io;
Ok, let's start writing procedures:
First we have to judge whether the user chooses to upload files, we can use the following to implement:
IF (file1.postedfile.contentLENGTH> 0)
If the user has the file uploaded, then:
String name = file1.postedfile.FileName; // Get the input file name INT i = name.lastIndexof ("."); string newname = name.substring (i); // Get the extension of the file // change below "C: //" to the address you want to save. DateTime now; string strname = now.year.tostring () now.month.tostring () NOW.DAY.TOSTRING () file1.postedfile.contentLENGTH.TOSTRING () newname; // pair file Rename it. The name is: year month day file size.
String path = "c: //" strname; // Set the location of the upload file, set to the virtual directory:
// String path = server.mappath ("userimage ///") strname; stored in the specified virtual directory IF (file.exists (path)) // does not have {label1.text = "this file name in the server There is already !! "; label2.text =" "; label3.text =" "; return;} file1.postedfile.saves (path); // Store file // Various properties of the upload file. Label1.text = "Upload File Name:" File1.postedFile.FileName; Label2.Text = "Upload File Type:" File1.postedFile.ContentType; Label3.Text = "Upload File Size:" File1.PostedFile.ContentLength .Tostring (); here, the uploaded program is finished, but there are some places to pay attention to:
To add attributes in Form: eNCTYPE = "Multipart / Form-Data" otherwise an error that does not reference an object will be referenced to an instance. The full program is as follows:
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls Using system.web.ui.htmlcontrols; using system.io;
Namespace myhomewebapp.uploadtest {///
#Region Web Form Designer Generated Code Override Protected Void OnNit (Eventargs E) {// // Codegen: This call is necessary for the ASP.NET Web Form Designer. // InitializeComponent (); base.onit (e);} ///
private void Button1_Click (object sender, System.EventArgs e) {try {if (File1.PostedFile.ContentLength> 0) {string name = File1.PostedFile.FileName; int i = name.LastIndexOf ( "."); string newname = Name.substring (i); // Change the following "C: //" to the address you want to save. DateTime now; string strname = now.year.tostring () now.month.toString () NOW.DAY.TOSTRING () file1.postedfile.contentLENGTH.TOSTRING () newname;
String path = "c: //" strname; if (file.exists (path)) {label1.text = "This file name already exists on the server!"; label2.text = "; label3.text = "";} File1.postedfile.saveas (path); // Various properties of the upload file are obtained. Label1.text = "Upload File Name:" File1.postedFile.FileName; Label2.Text = "Upload File Type:" File1.postedFile.ContentType; Label3.Text = "Upload File Size:" File1.PostedFile.ContentLength .Tostring ();}} catch (exception err) {label1.text = err.Message; label1.text = err.source; label3.text = err.helplink;}}}}