Asp. NET uploading pictures using File Upload controls

Source: Internet
Author: User

aspx code<%@ page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD"> "http://www.w3.org/1999/xhtml"> "Server"> <title> Uploading Images </title> "Form1"runat="Server"> <div> <asp:fileupload id="FileUpload1"runat="Server"/> <asp:button id="Btnfileupload"runat="Server"text="Upload"onclick="Btnfileupload_click"/> </div> </form> </body>

CS Codeusing System;
UsingSystem.Configuration; usingSystem.Data; usingSystem.Linq; usingsystem.web; usingSystem.Web.Security; usingSystem.Web.UI; usingSystem.Web.UI.HtmlControls; usingSystem.Web.UI.WebControls; usingSystem.Web.UI.WebControls.WebParts; usingSystem.Xml.Linq; Public Partial class_default:system.web.ui.page {protected voidPage_Load (Objectsender, EventArgs e) { } protected voidBtnfileupload_click (Objectsender, EventArgs e) {Boolean FileOK=false; //get the uploaded file name stringFileName = This. Fileupload1.filename; //Get Physical pathString Path = Server.MapPath ("~/images/"); //determine if the upload control uploads a file if(fileupload1.hasfile) {//Determine if the extension of the uploaded file is an allowed extension of ". gif", ". png", ". jpeg", ". jpg", ". BMP "String fileextension =System.IO.Path.GetExtension (fileName). ToLower (); String[] Extensions= {". gif",". PNG",". JPEG",". jpg",". bmp"}; for(inti =0; i < extensions.length; i++) { if(FileExtension = =Extensions[i]) {FileOK=true; } } } //if the upload file name extension is allowed, save the file in the directory specified on the server if(FileOK) {Try { This. FileUpload1.PostedFile.SaveAs (path +fileName); MessageBox ("File Upload Complete"); } Catch(Exception ex) {MessageBox ("The file cannot be uploaded because:"+Ex. Message); } } Else{MessageBox ("This type of file cannot be uploaded"); } } protected voidMessageBox (stringstr) {Page.ClientScript.RegisterStartupScript (Page.gettype (),"message","<script language= ' javascript ' Defer>alert ('"+str+"');</script>"); } }

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.