WebForm fileupload (upload button control) simple introduction and download, upload files when the picture preview

Source: Internet
Author: User
Tags server memory least privilege

1.FileUpload Upload Control: (Original: http://www.cnblogs.com/hide0511/archive/2006/09/24/513201.html) FileUploadcontrol 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 in the control's text box (for example, C:\MyFiles\TestFile.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 chooses which files to upload, FileUploadControl 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, a call is made in an event-handling method that raises an event that sends a postback to the server SaveAsMethod.
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.
There are three areas that need Note
1. Confirm that the file is included
In the call SaveAsmethod to save the file to the server before using theHasFileproperty to verify FileUploadThe control does contain files. If HasFileReturn true, the call SaveAsMethod. If it returns false, a message is displayed to the user indicating that the control does not contain a file. Do not pass the checkPostedFileproperty to determine whether the file to be uploaded exists, because the property contains 0 bytes by default. Therefore, even if FileUploadControl is empty, PostedFileProperty still returns a non-null value.
2. File Upload size limit
By default, the upload file size limit is 4096 KB (4 MB). Can be set by settinghttpRuntime ElementsOf maxRequestLengthproperty to allow uploading of larger files. To increase the maximum file size allowed for an entire application, set the value in the Web. config file maxRequestLengthProperty. To increase the maximum file size allowed for a specified page, set the LocationElement within the maxRequestLengthProperty.
When uploading a large file, the user may also receive the following error message:
aspnet_wp.exe (pid:1520) was recycled because memory consumption exceeded 460 MB (percent of available 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. Config file is the default configuration in the <processModel> configuration section. memorylimitThe default value of the property. 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 enable the application to obtain write access to the directory, you mustaspnethostingpermissionobject is granted to the application and its trust level is set toAspnethostingpermissionlevel.mediumValue. 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.

Common properties of the FileUpload control:

Property

Data type

Description

Filebytes

Byte[]

Gets the byte array of the uploaded file

Filecontent

Stream

Gets the stream object for the specified upload file

FileName

String

Gets the file name of the uploaded file on the client

HasFile

Bool

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 relevant properties of the uploaded file

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 calling the Httppostedfile object's ContentLength, and you can get the type of the uploaded file by invoking the ContentType property of the Httppostedfile object. 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).

WebForm fileupload (upload button control) simple introduction and download, upload files when the picture preview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.