图片上传预览

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;
        }
    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。