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 > ) {
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 > )
{
HttpPostedFile hpf = context.Request.Files[];
if (hpf.ContentLength > )
{
//获取文件名和扩展名
fileNameNo = Path.GetFileName(hpf.FileName);
//获取文件所在目录
DirectoryName = Path.GetDirectoryName(hpf.FileName);
//获取扩展名
Extension = Path.GetExtension(hpf.FileName);
if (AvailableFileType(Extension)) //文件格式符合
{
if (hpf.ContentLength <= * * ) //文件大小符合
{ //获取文件名(不包括扩展名)
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) == -) return false;
else return true;
} public bool IsReusable
{
get
{
return false;
}
}
}
}

一般处理程序获取Layui上传的图片的更多相关文章

  1. PHP之ThinkPHP框架(验证码、文件上传、图片处理)

     验证码 验证码是框架自带有的,比之前使用GD库简单方便许多,其实现原理基本相似,都是生成图片,保存验证码值到Session中,表单提交验证码,然后进行值的对比验证. 简单的显示: <form ...

  2. WebService上传下载图片

    WebService服务端 接受要上传的图片 public string UploadImg(byte[] fileBytes, int id) { try { string filePath = M ...

  3. vue+element-ui中的图片获取与上传

    vue+element-ui中的图片获取与上传 工作上接触了一下图片的处理,图片的格式是文件流, 记录如下. 请求图片 请求图片的时候,带上{ responseType: 'blob' }, 否则图片 ...

  4. layui上传文件组件(前后端代码实现)

    我个人博客系统上传特色图片功能就是用layui上传文件组件做的.另外采用某个生态框架,尽量都统一用该生态框架对应的解决方案,因为这样一来,有这么几个好处?1.统一而不杂糅,有利于制定相应的编码规范,方 ...

  5. thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印

    今天分享一下thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印.博主是新手,在这里卡住了很久(>_<) thinkphp 3.2.3整合ueditor 1.4 下载 ...

  6. ASP.NET、JAVA跨服务器远程上传文件(图片)的相关解决方案整合

    一.图片提交例: A端--提交图片 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string u ...

  7. KindEditor上传本地图片在ASP.NET MVC的配置

    http://www.cnblogs.com/upupto/archive/2010/08/24/1807202.html 本文解决KindEditor上传本地图片在ASP.NET MVC中的配置. ...

  8. .net mvc4 利用 kindeditor 上传本地图片

    http://blog.csdn.net/ycwol/article/details/41824371?utm_source=tuicool&utm_medium=referral 最近在用k ...

  9. MVC&WebForm对照学习:文件上传(以图片为例)

    原文  http://www.tuicool.com/articles/myM7fe 主题 HTMLMVC模式Asp.net 博客园::首页::  ::  ::  ::管理 5 Posts :: 0 ...

随机推荐

  1. numpy.unpackbits()

    numpy.unpackbits numpy.unpackbits(myarray, axis=None) Unpacks elements of a uint8 array into a binar ...

  2. 不使用if switch 各种大于 小于 判断2个数的大小

    哥们写的代码: dword big; __asm { mov eax,a mov ebx,b cmp eax,ebx jle HOHO big =ebx HOHO: big = eax } 网上搜了一 ...

  3. Loitor_产品(一)

    源码:https://github.com/loitor-vis/vi_sensor_sdk 注意:以下要一直在管理员权限 1.C++ 示例程序的编译步骤 先确认你的系统已经成功安装了OpenCV. ...

  4. db2 统计信息 runstats

    1.runstats的语法:runstats on table [模式名].[表名] with distribution and detailed indexes all注意:你可以在所有列上,或者仅 ...

  5. 骗分大法之-----分块||迷之线段树例题a

    什么是分块呢? 就是一种可以帮你骗到不少分的神奇的算法. 分块的写法有几种,我所知道的有①预处理②不预处理 不预处理的代码我看得一脸懵逼 所以我在这里就谈一下预处理的版本www 首先看一道题: 给定一 ...

  6. Java数据结构和算法(一)概念

    Java数据结构和算法(一)概念 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 一.逻辑结构 数据之间的相互关系称为逻辑结构 ...

  7. db2学习笔记

    a.服务端安装 v11.1_win64_expc.zip 官网下载 b.客户端安装 Toad for DB2 Freeware 6.1 百度找找 .建数据库 create database HRA_G ...

  8. js 数组去重的三种方法(unique)

    方法一: Array.prototype.unique=function(){ var arr=[];//新建一个临时数组 for(var i=0;i<this.length;i++){//遍历 ...

  9. DevExpress TextEdit Focus问题

    在标签切换时设置第一个TextEdit获取输入焦点无效,需要采用消息Post方式设置 //标签切换事件 xtraTabControl1.Selected += (s, e) => { if (e ...

  10. springboot问题,没有主清单属性

    在pom.xml中添加 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins< ...