Use. NET file control upload files to the final Solution (personal version)

Source: Internet
Author: User
Tags config file size file upload final ftp modify server memory
Personal Edition | resolution | control | Upload the piece of rubbish after traversing all the posts about uploading large files in csdn. (:-))


upload large files, there are several ways:


1, Shi HttpWorkerRequest method, too difficult to read:-(


2, use the control of third party aspnetupload want Money!! Forget it, we still like it for free.


3, modify Web.config file, but cannot catch error.


4, the way to upload via FTP. The server needs to provide an FTP service.


  


, had to choose a third way, who let us too stupid! (Too stupid, no relationship, to eat more fish, I heard that eating fish can be smart point.) )


  


then carefully study the third way of it!


  


Modify Webcong File:


<system.web>


<httpruntime maxrequestlength= "40690"


usefullyqualifiedredirecturl= "true"


executiontimeout= "6000"


usefullyqualifiedredirecturl= "false"


minfreethreads= "8"


minlocalrequestfreethreads= "4"


apprequestqueuelimit= "100"


enableversionheader= "true"


/>


</system.web>


  


which is closely related to the upload is:


maxRequestLength


indicates the maximum file upload size supported by asp.net.


This restriction can be used to prevent denial of service attacks caused by a user passing a large number of files to the server.


the specified size is in kilobytes.

The default value for
is 4096 KB (4 MB).


  


executiontimeout


indicates the maximum number of seconds allowed to execute a request before it is asp.net automatically closed.


unit for seconds, when uploading large files to set this larger.


  


  


if server memory is 512M, you can upload files of size 160M. (haven't tried, csdn the unanimous opinion of the public posts.) )


' www.knowsky.com


Here the Web.config setting is over.


but once the size of the uploaded file exceeds the file size of this setting, the following error occurs:


This page cannot display


the page you want to view is currently unavailable. Your web site may be experiencing technical problems, or you need to adjust your browser settings.


  


Although cannot solve, that also must catch this mistake! What do we do?


recently ate a few fish, think about it, because this error is caused by the file control foreground error, so in the background want to use Try...catch to capture is not feasible.


then thought of using. NET error capture page mechanism to deal with. Yes, it works.


  


1, first set web.config


<customerrors mode= "on"/>


2, create a new error.aspx file that is designed to trap errors.


3, add page directives to the front page of the ASPX page where the files are uploaded. Errorpage= "Uploaderror.aspx"


4, add some code in the error.aspx to determine if the error message is a foreground error caused by file.


public class UploadError:System.Web.UI.Page


   {


private void Page_Load (object sender, System.EventArgs e)


   {


Exception ex = Server.GetLastError ();


if (ex!= null)


   {


Response.Redirect (".. /error.aspx ");


   }


else//foreground error ex is null


   {


Response.Redirect ("uploadexcel.aspx?err=1"); Re-jump to upload page, plus err parameter to display error message


   }


   }


  


5, display error prompts.


public class Uploadexcel:System.Web.UI.Page


   {


private void Page_Load (object sender, System.EventArgs e)


   {


if (request["err"] = = "1")


   {


Page.registerstartupscript ("Budget", "<script language = Javascript>alert (' Upload file has!") File size is too large! ') </script> ");


   }


}


   }


  


6, finish the work. (The fish is not free)


  


after the above changes, now uploaded into the program to specify the size of the file, if more than, then pop-up error prompts.


this, so much better, at least they can accept, and the user explanation is not too strenuous.





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.