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. swift 移除所有子控件

    /// 移除所有子控件 func removeAllSubViews(){ if self.view.subviews.count>0{ self.view.subviews.forEach({ ...

  2. Raft 一致性算法论文译文

    本篇博客为著名的 RAFT 一致性算法论文的中文翻译,论文名为<In search of an Understandable Consensus Algorithm (Extended Vers ...

  3. PAT 1074 宇宙无敌加法器(20)(代码+思路+测试点分析)

    1074 宇宙无敌加法器(20 分)提问 地球人习惯使用十进制数,并且默认一个数字的每一位都是十进制的.而在 PAT 星人开挂的世界里,每个数字的每一位都是不同进制的,这种神奇的数字称为"P ...

  4. PAT 1064 朋友数(20)(代码)

    1064 朋友数(20 分) 如果两个整数各位数字的和是一样的,则被称为是"朋友数",而那个公共的和就是它们的"朋友证号".例如 123 和 51 就是朋友数, ...

  5. [SoapUI] 重载JSONComparator比对JSON Response,忽略小数点后几位,将科学计数法转换为普通数字进行比对,在错误信息中打印当前循环的case number及其他附加信息

    重载JSONComparator比对JSON Response,忽略小数点后几位,将科学计数法转换为普通数字进行比对,在错误信息中打印当前循环的case number及其他附加信息 package d ...

  6. 彻底测试全部拷贝list相关操作的区别python

    1.用浅拷贝后修改数字,可以起到与原数据分离的效果 import copy origin = [, , [, ]] #origin 里边有三个元素:, ,[, ] cop1=origin.copy() ...

  7. windows 安装配置jdk7

    1.安装jdk这里不在介绍 2.配置新建用户变量:JAVA_HOME 值为(就是你自己jdk的安装路径):C:\Program Files\Java\jdk1.7.0_75\ 3.配置系统变量:Pat ...

  8. ipad The data couldn’t be read because it isn’t in the correct format

    原来是land left和land right都勾选的,去掉land left后出现这个问题

  9. phalApi数据库操作

    在很多时候,我们会遇到数据库表里面的某个值需要+1操作,我们不能简单地在update的时候写入array('key' => 'key+1'),因为在解析sql的时候,key+1 会带上引号作为一 ...

  10. 用org.mybatis.generator 生成代码

    1:引入pom 2:增加生成配置xml: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...