一般處理程序獲取Layui上傳的圖片


asp.net利用一般處理程序獲取用戶上傳的圖片,上傳圖片利用的layui

 

前台頁面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="CacheTest.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    //需要引入layui需要的css和js文件
    <link href="layui/css/layui.css" rel="stylesheet" />
    <script src="layui/layui.js"></script>
    <script>
        layui.use('upload', function () {
            var $ = layui.jquery, upload = layui.upload;

            //普通圖片上傳
            var uploadInst = upload.render({
                elem: '#test1',
                url: '一般處理程序',
                auto: false,
                BindAction: "#按鈕",
                choose: function(ohj) {//點擊上傳前預覽
                    //預讀本地文件示例,不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#demo1').attr('src', result); //圖片鏈接(base64)
                    });
                },
                before: function (obj) {
                    //預讀本地文件示例,不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#demo1').attr('src', result); //圖片鏈接(base64)
                    });
                },
                done: function (res) {//完成事件
                    //如果上傳失敗
                    if (res.code > 0) {
                        return layer.msg('上傳失敗');
                    }
                    //上傳成功
                },
                error: function () {
                    //演示失敗狀態,並實現重傳
                    var demoText = $('#demoText');
                    demoText.html(
                        '<span style="color: #FF5722;">上傳失敗</span> <a class="layui-btn layui-btn-mini demo-reload">重試</a>');
                    demoText.find('.demo-reload').on('click',
                        function () {
                            uploadInst.upload();
                        });
                }
            });
        })
    </script>
</head>
<body>
    <form id="form1">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
            <legend>常規使用:普通圖片上傳</legend>
        </fieldset>

        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test1">上傳圖片</button>
            <div class="layui-upload-list">
                <img class="layui-upload-img" id="demo1" style="width:200px">
                <p id="demoText"></p>
            </div>
        </div>
    </form>
</body>
</html>

一般處理程序 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace WebApplication1
{
    /// <summary>
    /// Handler1 的摘要說明
    /// </summary>
    public class Handler1 : IHttpHandler
    {

        string fileNameNo = "";
        string DirectoryName = "";
        string Extension = "";
        string fileName = "";
        string fullPath = "";
        string uploadDown = "";
        string savePath = "";
        string netPath = "";
        string parm = "";
        StringBuilder msg = new StringBuilder();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string physicalPath = System.Web.HttpContext.Current.Server.MapPath("/Upload/");
            //parm = context.Request.QueryString["id"].ToString();
            HttpFileCollection hfc = context.Request.Files;

            if (hfc.Count > 0)
            {
                HttpPostedFile hpf = context.Request.Files[0];
                if (hpf.ContentLength > 0)
                {
                    //獲取文件名和擴展名 
                    fileNameNo = Path.GetFileName(hpf.FileName);
                    //獲取文件所在目錄 
                    DirectoryName = Path.GetDirectoryName(hpf.FileName);
                    //獲取擴展名 
                    Extension = Path.GetExtension(hpf.FileName);
                    if (AvailableFileType(Extension)) //文件格式符合
                    {
                        if (hpf.ContentLength <= 1024 * 1024 * 4) //文件大小符合
                        {

                            //獲取文件名(不包括擴展名)
                            fileName = Path.GetFileNameWithoutExtension(hpf.FileName);
                            string newFileName = Guid.NewGuid().ToString().Replace("-", "") + Extension;
                            uploadDown = physicalPath + newFileName;
                            netPath = "../upload/" + newFileName;
                            savePath = Path.Combine(physicalPath, newFileName);
                            hpf.SaveAs(savePath);
                            msg.Append(
                                "{\"isok\":\"true\",\"username\":\"\",\"createtime\":\"\",\"message\":\"上傳成功\",\"sourcefilename\":\"" +
                                context.Request.RawUrl + "\",\"netfilename\":\"" + netPath + "\",\"fileid\":\"" +
                                newFileName + "\"}");
                            context.Response.Write(msg.ToString());
                            //獲取文件的絕對路徑 
                            //string fullPath = Path.GetFullPath(FileUploadImg.PostedFile.FileName);
                            ////獲取文件所在地分區 
                            //string PathRoot = Path.GetPathRoot(FileUploadImg.PostedFile.FileName); 
                        }
                        else
                        {
                            msg.Append(
                                "{\"isok\":\"false\",\"username\":\"\",\"createtime\":\"\",\"message\":\"上傳失敗,上傳文檔不能大於4MB!\",\"sourcefilename\":\"\",\"netfilename\":\"\",\"fileid\":\"\"}");
                            context.Response.Write(msg.ToString());
                        }
                    }
                    else //文件格式不符合
                    {
                        msg.Append(
                            "{\"isok\":\"false\",\"username\":\"\",\"createtime\":\"\",\"message\":\"文件格式不支持,支持bmp,jpg,gif,png,rar,zip,doc,xls,docx,xlsx格式的上傳!\",\"sourcefilename\":\"\",\"netfilename\":\"\",\"fileid\":\"\"}");
                        context.Response.Write(msg.ToString());
                    }
                }
                else
                {
                    msg.Append(
                        "{\"isok\":\"false\",\"username\":\"\",\"createtime\":\"\",\"message\":\"請選擇上傳的文件\",\"sourcefilename\":\"\",\"netfilename\":\"\"}");
                    context.Response.Write(msg.ToString());
                }
            }
            else
            {
                msg.Append(
                    "{\"isok\":\"false\",\"username\":\"\",\"createtime\":\"\",\"message\":\"請選擇上傳的文件\",\"sourcefilename\":\"\",\"netfilename\":\"\"}");
                context.Response.Write(msg.ToString());
            }
        }
        public bool AvailableFileType(string temp)
        {
            string[] TypeBag = { ".bmp", ".jpg", ".gif", ".png", ".rar", ".zip", ".doc", ".xls", ".docx", ".xlsx" };
            if (Array.IndexOf(TypeBag, temp) == -1) return false;
            else return true;
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM