標籤:
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; } }}
圖片上傳預覽