zoukankan      html  css  js  c++  java
  • 图片上传预览

    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;  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;
            }
        }
    }
  • 相关阅读:
    上传视频到七牛云django端实现
    课程全文检索接口
    搜索引擎工作原理
    创建订单并生成支付链接接口
    支付宝支付流程
    通过课程查询商品信息
    如何使用 RESTClient 调试微信支付接口
    关于HTML使用ComDlg ActiveX 无法弹出相应对话框的问题1
    Android自定义View的实现方法,带你一步步深入了解View(四)
    Android视图状态及重绘流程分析,带你一步步深入了解View(三)
  • 原文地址:https://www.cnblogs.com/engine/p/4275490.html
Copyright © 2011-2022 走看看