The FileUpload control displays a text box control and a browse button that allows the user to select a file on the client and upload it to the WEB server. The user enters the full path of the file on the local computer (for example, C:\MyFiles\test) in the control's text box. TXT) to specify the file to upload. Users can also select a file by clicking the browse button and then locating the file in the Select File dialog box.
After the user selects a file to upload, theFileUpload control does not automatically save the file to the server. You must explicitly provide a control or mechanism that enables the user to submit the specified file. For example, you could provide a button that the user can click to upload a file. The code written to save the specified file should call the SaveAs method, which saves the contents of the file to the specified path on the server. Typically, the SaveAs method is called in an event-handling method that raises an event that sends a postback to the server.
During file upload, the file data is uploaded and cached in the server's memory as part of the page request, and then written to the server's physical hard disk.
Several public read-only properties commonly used by FileUpload controls
Name |
return type |
Description |
Filecontent |
Stream |
Returns a Stream object that points to the upload file |
FileName |
String |
Returns the file name of the uploaded file on the client, without path information |
HasFile |
Boolean |
Gets a Boolean value that indicates whether the FileUpload control already contains a file |
PostedFile |
Httppostedfile |
Gets a Httppostedfile object associated with the uploaded file that can be used to obtain the associated properties of the uploaded file. The following table lists the read-only properties that it exposes |
Httppostedfile Property
Name |
return type |
Description |
ContentLength |
Integer |
Returns the file size in bytes of the uploaded file |
ContentType |
String |
Returns the MIME content type of the uploaded file |
FileName |
String |
Returns the fully qualified name of the file on the client |
InputStream |
Stream |
Returns a Stream object that points to the upload file |
There are three areas to note :
1. Confirm that the file is included
Before calling the SaveAs method to save the file to the server, use the HasFile property to verify that the FileUpload control does contain the file. If HasFile returns true, the SaveAs method is called. If it returns false, a message is displayed to the user indicating that the control does not contain a file. Do not determine if the file to be uploaded exists by checking the PostedFile property, because the property contains 0 bytes by default. Therefore, even if the FileUpload control is empty, thePostedFile property still returns a non-null value.
2. File Upload size limit
By default, the upload file size limit is 4096 KB (4 MB). You can allow larger files to be uploaded by setting the maxRequestLength property of the httpRuntime element .
The related nodes are as follows:
〈system.web>
〈httpruntime maxrequestlength= "40690" executiontimeout= "6000"/>
〈/system.web>
maxRequestLength represents the maximum number of files that can be uploaded, executiontimeout represents the upload seconds allowed before ASP.
To increase the maximum file size allowed for an entire application, set the maxRequestLength property in the Web. config file. To increase the maximum file size allowed for a specified page, set the maxRequestLength property within the location element in Web. config.
When uploading a large file, the user may also receive the following error message:
Was recycled because memory consumption exceeded 460 MB (percent RAM).
The above information indicates that the size of the uploaded file cannot exceed 60% of the server memory size. The 60% here is the web. The default configuration for the config file is the default value for the memorylimit property in the <processModel> configuration section. Although can be modified, but if the larger the upload file, the smaller the chance of success, is not recommended to use.
3. Write permission for the upload folder
There are two ways in which applications can gain write access. You can explicitly grant the account used to run the application by writing access to the directory where you want to save the uploaded file. You can also increase the level of trust granted to ASP. To give your application write access to perform a directory, you must grant the aspnethostingpermission object to the application and set its trust level to The Aspnethostingpermissionlevel.medium value. Increasing the trust level increases the application's access to server resources. Note that this method is not secure, because if a malicious user controls the application, he or she can run the application at a higher level of trust. It is a best practice to run an ASP. NET application in the context of a user with only the least privilege required to run the application.
There are 3 ways to access the upload file:
1. Pass the Filebytes property. This property places the uploaded file data in a byte array and iterates through the array, enabling you to understand the contents of the uploaded file in bytes.
2. Pass the Filecontent property. Call this property to get a stream object that points to the upload file. You can use this property to read the uploaded file data and use the Filebytes property to display the file contents.
3. Pass the PostedFile property. Call this property to obtain a Httppostedfile object associated with the uploaded file that can be used to obtain information related to uploading the file. For example, you can get the upload file size by invoking the contentlength of the Httppostedfile object, and you can get the type of the uploaded file by invoking the Httppostedfile object ContentType property. Call the FileName property of the Httppostedfile object to get the full path of the uploaded file on the client (call the FileName property of the FileUpload control and only get the file name).
FileUpload control Upload a simple instance:
C # codeCopy
Front desk Just drag a FileUpload control and a button control
<asp:fileupload id= "FileUpload1" runat= "Server"/><br/>
<asp:button id= "Button1" runat= "Server" onclick= "Button1_Click" text= "button"/>
Background
Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
public partial class Manage_News_upload:System.Web.UI.Page
{protected void Page_Load (object sender, EventArgs e){} protected void Button1_Click (object sender, EventArgs e){if (fileupload1.hasfile){String Fileexrensio = System.IO.Path.GetExtension (fileupload1.filename). ToLower ();//tolower converted to lowercase string FileType = FileUpload1.PostedFile.ContentType; String uploadurl = Server.MapPath ("~/upload/");//uploaded directory if (FileType = = "Image/bmp" | | FileType = = "Image/gif" | | FileType = = "Image/jpeg" | | FileType = = "Image/jpg" | | FileType = = "Image/png")//Determine file type{Try {if (! System.IO.Directory.Exists (Uploadurl))//Determine if the folder already exists {System.IO.Directory.CreateDirectory (uploadurl);//Create Folder } FileUpload1.PostedFile.SaveAs (Uploadurl + fileupload1.filename); } catch {Response. Write ("failed"); }} else {Response.Write ("Malformed"),}} else Response.Write ("Please select File"); }}
The above example also has a lot of properties are not used, you can add their own property to judge according to their own needs, write up to meet their own project upload method.