ASP. NET fileupload

Source: Internet
Author: User

CopyCode The Code is as follows: <Table Style = "width: 100%">
<Tr>
<TD>
<Asp: validationsummary id = "validationsummary1" runat = "server"/>
<Br/>
<Asp: fileupload id = "fileupload1" runat = "server"/>
<Asp: button id = "btn_upload" runat = "server" onclick = "btn_upload_click"
TEXT = "Upload"/>

<Asp: customvalidator id = "customvalidator1" runat = "server"
Controltovalidate = "fileupload1" display = "static"
Errormessage = "You shocould only can upload image file such as files with. jpg or GIF extension"
Onservervalidate = "image_validate"> * </ASP: customvalidator>
</TD>
</Tr>
</Table>

Add to code behind CS File Copy code The Code is as follows: using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. IO;
Using system. drawing;
Public partial class practice_fileupload: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
Protected void btn_upload_click (Object sender, eventargs E)
{
If (page. isvalid)
{
String Path = @ page. mappath ("user_edit.aspx"). Replace ("user_edit.aspx", "") + "Documents \\";
String S = path + session ["username"]. tostring ();
If (! System. Io. Directory. exists (path + session ["username"]. tostring ()))
{
System. Io. Directory. createdirectory (path + session ["username"]. tostring ());
}
If (fileupload1.hasfile)
{
Fileupload1.saveas (server. mappath ("~ /Seeker/documents/"+ session [" username "]. tostring () +"/"+ this. fileupload1.filename ));
}
}
}
Protected void image_validate (Object source, servervalidateeventargs ARGs)
{
String fileext = path. getextension (fileupload1.filename). tolower ();
String filename = path. getfilename (fileupload1.filename );
If (fileext! = ". Jpg" & fileext! = ". GIF ")
{
Args. isvalid = false;
}
}
Protected void customvalidator2_servervalidate (Object source, servervalidateeventargs ARGs)
{
Bitmap BMIP = new Bitmap (fileupload1.postedfile. inputstream );
If (BMIP. width> 100 | BMIP. Height> 100)
{
Args. isvalid = false;
}
Else
{
Args. isvalid = true;
}
}
}

The default size of files uploaded by the fileupload control is 4 MB. This solution was found from the Internet.
It is worth noting that the maximum size of the file to be uploaded by default is 4 MB. This is found online.
If you want to add one, you can modify it in machine. config. Copy code The Code is as follows: Executiontimeout = "110" [in seconds] [number
Maxrequestlength = "4096" [number]
Requestlengthdiskthreshold = "80" [number]
Usefullyqualifiedredirecturl = "false" [true | false]
Minfreethreads = "8" [number]
Minlocalrequestfreethreads = "4" [number]
Apprequestqueuelimit = "5000" [number]
Enablekerneloutputcache = "true" [true | false]
Enableversionheader = "true" [true | false]
Apartmentthreading = "false" [true | false]
Requirerootedsaveaspath = "true" [true | false]
Enable = "true" [true | false]
Sendcachecontrolheader = "true" [true | false]
Shutdowntimeout = "90" [in seconds] [number]
Delaynotificationtimeout = "5" [in seconds] [number]
Waitchangenotification = "0" [number]
Maxwaitchangenotification = "0" [number]
Enableheaderchecking = "true" [true | false]
/>

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.