One, aspx page:
<%@ page language= "C #" autoeventwireup= "true" codebehind= "FileUploadDemo.aspx.cs" inherits= " Webapplication1.fileuploaddemo "%><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> Two, aspx backend code:
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using system.web.ui.webcontrols;using system.io;namespace webapplication1{public partial class Fileuploaddemo: System.Web.UI.Page {protected void Page_Load (object sender, EventArgs e) {Btnuploadfile.serverclick + = new E Venthandler (Btnuploadfile_serverclick); } void Btnuploadfile_serverclick (object sender, EventArgs e) {if (this.fileUpload.HasFile) {Strin G fileName = This.fileUpload.PostedFile.FileName; Client file path String extension = System.IO.Path.GetExtension (fileName); if (extension. ToLower ()! = ". jpg" && extension. ToLower ()! = ". png") {Scriptmanager.registerstartupscript (this, this. GetType (), "MSG", "alert (' only allow JPG and png! '); ", true); Return } string pathbase = "D:\\uploadfile"; if (! Directory.Exists (Pathbase)) directory.createdirectory (pathbase); String weBfilepath = Path.Combine (Pathbase, fileName); Database save file path (relative full path) this.fileUpload.SaveAs (Webfilepath); Use the SaveAs method to save the file Scriptmanager.registerstartupscript (this, this. GetType (), "MSG", "alert (' Upload succeeded!") '); ", true); } } }}
Change the display mode of fileupload file upload control, and automatically upload after selecting files