ASPX code:
<%@ Page Language="C #"AutoEventWireup="true"CodeFile="Default2.aspx.cs"Inherits="DEFAULT2" %><!DOCTYPE HTML><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Headrunat= "Server"><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/> <title></title></Head><Body> <formID= "Form1"runat= "Server"> <Div> <BR/> <Asp:fileuploadID= "FileUpload1"runat= "Server" /> <Asp:buttonID= "Button1"runat= "Server"OnClick= "Button1_Click"Text= "Upload" /> <BR/> <BR/> <BR/> <DivID= "Localimag"style= "width:200px; height:200px"> <imgID= "Preview"alt= "Preview picture"src= "Images/qiye.png"width= "$"Height= "$" /> <Asp:fileuploadID= "Picload"Width= "200px"Height= "200px"style= "position:relative; top:-200px; opacity:0"runat= "Server"onchange= "Javascript:setimagepreview (This,localimag,preview);"></Asp:fileupload> </Div> <Asp:buttonID= "Button2"runat= "Server"OnClick= "Button2_Click"Text= "Upload" /> <Scripttype= "Text/javascript"> functionSetimagepreview (docobj, Localimagid, Imgobjpreview) {if(Docobj.files&&docobj.files[0]) { //under Firefox, set the IMG property directlyImgObjPreview.style.display= 'Block'; ImgObjPreview.style.width= '200px'; ImgObjPreview.style.height= '200px'; //Firefox 7 or above can not be used in the above Getasdataurl () way to get, need a wayimgobjpreview.src=window. Url.createobjecturl (docobj.files[0]); } Else { //under IE, use filtersDocobj.select (); varimgsrc=Document.selection.createRange (). text; //Initial size must be setLocalImagId.style.width= "200px"; LocalImagId.style.height= "200px"; //image exception capture to prevent users from modifying the suffix to forge a picture Try{LocalImagId.style.filter= "Progid:DXImageTransform.Microsoft.AlphaImageLoader (Sizingmethod=scale)"; LocalImagId.filters.item ("DXImageTransform.Microsoft.AlphaImageLoader"). SRC=imgsrc; } Catch(e) {alert ("the image you uploaded is not formatted correctly, please re-select!"); return false; } ImgObjPreview.style.display= 'None'; Document.selection.empty (); } return true; } </Script> <BR/> </Div> </form></Body></HTML>
CS Code:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls; Public Partial classdefault2:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) { } protected voidButton1_Click (Objectsender, EventArgs e) { //1. Locate the file name you want to upload stringName =FileUpload1.FileName.ToString (); //2. Processing file names stringClname = DateTime.Now.ToString ("yyyymmddhhmmssms")+name; //3. Create a path to save stringPath =@"files\"+Clname; //4, the processing path is the absolute path stringJdpath =Server.MapPath (path); //5. SaveFileupload1.saveas (Jdpath); } protected voidButton2_Click (Objectsender, EventArgs e) { //1. Locate the file name you want to upload stringName =FileUpload1.FileName.ToString (); //2. Processing file names stringClname = DateTime.Now.ToString ("yyyymmddhhmmssms") +name; //3. Create a path to save stringPath =@"images\"+Clname; //4, the processing path is the absolute path stringJdpath =Server.MapPath (path); //5. SaveFileupload1.saveas (Jdpath); }}
After uploading:
20151227:web: Uploading Files