圖片上傳預覽

來源:互聯網
上載者:User

標籤:

html 代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head id="Head1" runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>        <table>            <tr>                <td style="vertical-align: top; width: 10%;">                    <fieldset>                        <legend>選擇圖片</legend>                        <asp:FileUpload ID="FileUpload1" runat="server" />                    </fieldset>                </td>                <td>                    <fieldset>                        <legend>預覽</legend>                        <asp:Image ID="Image1" Width="100px" Height="100px" runat="server" Visible="false" />                    </fieldset>                </td>            </tr>                 </table>    </div>    </form></body></html>

後台cs代碼:

using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.IO;using System.Xml.Linq;public partial class Default2 : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        if (IsPostBack)        {            var ctrl = Request.Params[Page.postEventSourceID];            var args = Request.Params[Page.postEventArgumentID];            OnchangeHandle(ctrl, args);        }    }    /// <summary>    /// 繫結檔案選擇變更事件    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    protected void Page_Init(object sender, EventArgs e)    {        this.FileUpload1.Attributes.Add("onchange", Page.ClientScript.GetPostBackEventReference(this.FileUpload1, "onchange"));    }    /// <summary>    /// 檔案選擇變更事件    /// </summary>    /// <param name="ctrl"></param>    /// <param name="args"></param>    private void OnchangeHandle(string ctrl, string args)    {        if (ctrl == this.FileUpload1.UniqueID && args == "onchange")        {            this.Image1.Visible = true;            Session["UploadBytes"] = this.FileUpload1.FileBytes;            this.Image1.ImageUrl = "~/Class1.axd";        }    }    /// <summary>    /// 上傳按鈕事件    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    protected void btn_Onclick(object sender, EventArgs e)    {        //建立儲存的目錄        string directory = "Myfiles/";        //判斷目錄是否存在        if (!Directory.Exists(Server.MapPath(directory)))        {            //如果不存在,建立它            Directory.CreateDirectory(Server.MapPath(directory));        }        //新檔案        string newFile = Server.MapPath(directory + Guid.NewGuid().ToString() + ".jpg");        if ((Session["UploadBytes"]) != null)        {            byte[] buffer = (byte[])(Session["UploadBytes"]);            File.WriteAllBytes(newFile, buffer);        }    }}

web.Config

<httpHandlers>       <add verb="*" path="Class1.axd" type="Class1"/></httpHandlers>

Class1.axd指向的cs代碼:

using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Web.SessionState;/// <summary>///Class1 的摘要說明/// </summary>public class Class1 : IHttpHandler, IRequiresSessionState{    public Class1()    {        //        //TODO: 在此處添加建構函式邏輯        //    }    public void ProcessRequest(HttpContext context)    {        //Checking whether the UploadBytes session variable have anything else not doing anything        if ((context.Session["UploadBytes"]) != null)        {            byte[] buffer = (byte[])(context.Session["UploadBytes"]);            context.Response.BinaryWrite(buffer);        }    }    public bool IsReusable    {        get        {            return false;        }    }}

 

圖片上傳預覽

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.