ASP. NET FileUpload controls upload pictures and preview pictures

Source: Internet
Author: User

Page code:

<form id="Form1"runat="Server"> <div> <asp:fileupload id="FileUpload1"runat="Server"/> <asp:button id="Button1"runat="Server"text="Upload"Width="54px"onclick="Button1_Click"/> <asp:label id="Label1"runat="Server"text=""style="color:red"></asp:Label> <asp:image runat="Server"Id="Image1"style="z-index:102; left:20px; Position:absolute ;Top:49px"width="73px"/></div> </form>

Background code:

Using system;using system.data;using system.configuration;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;namespace web.file{public partial class WebForm1:System.Web.UI.Page {protected void page_l Oad (object sender, EventArgs e) {} #region File upload///<summary>//File upload///</summary> protected void B Utton1_click (object sender, EventArgs e) {if (Fileupload1.filename = = "") {this.    Label1.Text = "Upload file cannot be empty";   Return   } bool Fileisvalid = FALSE; If the upload file is confirmed, determine if the file type meets the requirements if (this. Fileupload1.hasfile) {//Gets the suffix of the uploaded file String fileextension = System.IO.Path.GetExtension (this. Fileupload1.filename).    ToLower ();    String[] restrictextension = {". gif", ". jpg", ". bmp", ". png"}; Determine if the file type meets the requirements for (int i = 0; i < restrictextension.length; i++) {if (fileextension = = Restrictextension[i ]) {fileisvalid = TruE }//If the file type meets the requirements, call the SaveAs method to implement the upload and display the relevant information if (Fileisvalid = = true) {//upload file is greater than 10M if (fileupload1.post Edfile.contentlength > (Ten * 1024x768)) {this.       Label1.Text = "Upload file too large";      Return } try {this.       Image1.imageurl = "~/file/" + fileupload1.filename; This.       Fileupload1.saveas (Server.MapPath ("~/file/") + Fileupload1.filename); This.      Label1.Text = "File upload succeeded!"; } catch {this. Label1.Text = "File upload failed!"      "; } finally {}} else {this.     Label1.Text = "Only files with the. gif,.jpg,.bmp,.png suffix can be uploaded"; }}}} #endregion}}

Web. config configuration:

<!--because FileUpload control uploads up to 4M, if you want to upload larger files, change the size of maxRequestLength--><configuration> <system.web> < Compilation debug="true"targetframework="4.0"/> "2.0"Maxrequestlength="10485760"executiontimeout="3600"apprequestqueuelimit="10000"/> </system.web></configuration>

ASP. NET FileUpload controls upload pictures and preview pictures

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.