ASP. NET File Upload
Popular cloud disks, such as Baidu cloud disk, 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.
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "inputfile. aspx. cs" Inherits = "InputFile. inputfile" %>File Upload
The page background code is as follows:
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 );}
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: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20150104/20150104092504319.png" alt = "">
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.