ASP. NET file upload, asp.net practice

Source: Internet
Author: User

ASP. NET file upload, asp.net practice

Popular cloud disks, such as Baidu cloud disk, 360 cloud disk, and Kingsoft cloud disk, I believe they have been used. cloud disks are Internet storage tools and products of Internet Cloud technology. They provide enterprises and individuals with data storage, reading, and downloading services over the Internet, its two major features are security, stability, and mass storage. my laptop only has 500 GB hard drive. One day, when I found four of my six hard drive partitions red, I began to "move" to my Baidu cloud disk ", this is what we will talk about today: "File Upload ".


In fact, file upload is very common in Internet applications and website development. When we use "photo upload" in various social media software, uploading photos is essentially a file upload, but this file is an image or photo. after talking about this, I just want to express that the file upload function has a wide range of application fields, so it is also a very important technology in Web development. although I don't know how the File Upload Technology of various network disks is implemented, I believe the general principle is the same.

I recently studied ASP. NET technology, today it is used to simulate the file upload function, just for learning reference, not used as commercial development technology, do not really. the specific implementation process is as follows:

Create an ASP. NET application project, add a Web form, and set the name as needed, as shown in:


Then. the aspx file is used for page and design and code writing. Here, an asp control, called the FileUpload control, is more important. it is mainly used to call system APIs to browse local files and then add and upload files. here, we must create a folder to store files uploaded from the client. For example, I created an UpFile folder under the project file of the program, as shown in


The following describes the front-end and back-end Code. For example, the page layout is not so complex. Let's look at the page code first for three controls.

<Span style = "font-size: 18px;"> <% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "inputfile. aspx. cs "Inherits =" InputFile. inputfile "%> <! DOCTYPE html> <link href = "filecss.css" rel = "stylesheet"/> 

The page background code is as follows:

<Span style = "font-size: 18px;"> protected void button#click (object sender, EventArgs e) {// obtain the absolute path string FullFileName = this. fileUpload1.PostedFile. fileName; // The string truncation function is used to propose the file name to string fileName = FullFileName. substring (FullFileName. lastIndexOf ("\") + 1); // use the control method to save the file to the specified folder. this. fileUpload1.PostedFile. saveAs (Server. mapPath ("UpFile") + "\" + fileName) ;}</span>

Finally, let's look at the running result interface. I upload images, documents, video files, and other files in other formats, such as zip files, to test whether the upload is successful. any format file can be uploaded, but a problem occurs during the test, that is, the processing of large files.

Of course, during the test, when I upload a video file of over 20 mb, an error is reported. As for the larger file, it cannot be completed.

I found a solution on the Internet and found that the control has a limit on the file size. The default value is 4 MB. How can I modify it? You only need to make the following settings in the httpruntime tag of system. web in the configuration file web. config:


MaxRequestLength indicates the maximum file upload size supported by ASP. NET. This restriction can be used to prevent DoS attacks caused by a large number of files being transferred to the server. The specified size is in KB. The default value is 4096KB (4 MB ).

ExecutionTimeout indicates the maximum time allowed for request execution, in seconds. The default value is 90 seconds.

Summary: this is a simple example, but it is a very useful thing. No matter how high the technology is, it is more perfect on the basis of these. Therefore, it is important to lay a good foundation.

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.