asp.net fileupload Upload Picture examples _ practical skills

Source: Internet
Author: User
Copy Code code 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 should 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 code 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 > | bmip.height > 100)
{
Args. IsValid = false;
}
Else
{
Args. IsValid = true;
}
}
}

The default size of files uploaded by the FileUpload control is 4MB. This solution is found from the Internet.
It is worth noting that the default upload file for the FileUpload maximum of 4MB. It was found on the Internet.
If you want to increase, you can make changes in Machine.config
Copy Code code as follows:

executiontimeout = "a" [in Seconds][number
Maxreq Uestlength = "4096" [number]
Requestlengthdiskthreshold = "[number]
useFullyQualifiedRedirectUrl =" false " [True|false]
minFreeThreads = "8" [number]
minLocalRequestFreeThreads = "4" [number]
appRequestQueueLimit = "5000" [num BER]
Enablekerneloutputcache = "true" [True|false]
Enableversionheader = "true" [True|false]
Apartmentth Reading = "false" [True|false]
requireRootedSaveAsPath = "true" [True|false]
Enable = "true" [True|false] sendcachecontrolheader = "true" [True|false]
shutDownTimeout = "[in Seconds][number]
Delaynotificationti Meout = "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.