asp.net common file Upload method 1th/2 page _ Practical Tips

Source: Internet
Author: User
The main contents of this article include:
1, how to solve the file upload size limit
2. Save to server in file form
3. Convert to binary byte stream save to database and download method
4, upload resources on the Internet

The first part:
First, let's say how to solve the problem of file upload size limit in asp.net, we know that by default asp.net file upload size is limited to 2M, in general, we can use the change Web.config file to customize the maximum file size, as follows:
Copy Code code as follows:



The maximum size of the uploaded file becomes 4M, but this does not allow us to extend the maxRequestLength value indefinitely because ASP.net will load all the files into memory and then process them. The solution is to use the implied httpworkerrequest, using its getpreloadedentitybody and Readentitybody methods to read data in chunks from the pipe created by IIS for asp.net. The implementation method is as follows:
Copy Code code 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 will solve the problem of uploading large files.

Part II:
Here's how to upload a file from a client to a server
Copy Code code 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 name extension
</summary>
public string FileExtension
{
Get
{
return fileextension;
}
Set
{

FileExtension = value;
}

}
private string fileextension;
}

current 1/2 page   1 2 Next read the full text

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.