20151227:web: Uploading Files

Source: Internet
Author: User

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" />&nbsp;<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

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.