ASP. NET Common File Upload method page 1/2

Source: Internet
Author: User

The main content of this article includes:
1. How to Solve the file upload size limit
2. Save the file to the server
3. convert to a binary byte stream, save it to the database, and download it
4. Upload Internet Resources

Part 1:
First, let's talk about how to solve ASP. net file upload size restrictions, we know that by default ASP.. Net file upload size is limited to 2 MB. In general, we can change the web. the config file defines the maximum file size as follows:CopyCodeThe Code is as follows:

In this way, the maximum size of the uploaded file is 4 MB, but this does not allow us to infinitely expand the value of maxrequestlength, because ASP. NET will load all the files into the memory and then process them. The solution is to use the implicit httpworkerrequest and its getpreloadedentitybody and readentitybody methods to read data from the pipe created by IIS for ASP. NET in blocks. The implementation method is as follows:Copy codeThe Code is as follows: iserviceproviderprovider = (iserviceprovider) httpcontext. Current;
Httpworkerrequestwr = (httpworkerrequest) provider. getservice (typeof (httpworkerrequest ));
Byte [] BS = Wr. getpreloadedentitybody ();
.
If (! Wr. isentireentitybodyispreloaded ())
{
INTN = 1024;
Byte [] bs2 = newbyte [N];
While (WR. readentitybody (bs2, n)> 0)
{
..
}
}

This solves the problem of uploading large files.

Part 2:
Next we will introduce how to upload a client file to the server as a fileCopy codeThe Code is as follows: public class fileupload
{
Public fileupload ()
{

}
/**/
/// <Summary>
/// Upload File Name
/// </Summary>
Public String filename
{
Get
{
Return filename;
}
Set
{
Filename = value;
}
}
Private string filename;

/**/
/// <Summary>
/// Upload File Path
/// </Summary>
Public String filepath
{
Get
{
Return filepath;
}
Set
{
Filepath = value;
}
}
Private string filepath;

/**/
/// <Summary>
/// File Extension
/// </Summary>
Public String fileextension
{
Get
{
Return fileextension;
}
Set
{

Fileextension = value;
}

}
Private string fileextension;
}

Related Article

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.