表单元素file设置隐藏,通过其他元素打开:

.imgfile为input file

 $(".ul").click(function () {return $(".imgfile").click();
});

JS部分:

$(".imgfile").change(function () {
var file = $(".imgfile").get(0).files[0];
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onloadend = function () {
$("#Image1").attr("src", reader.result);
}
}); $("#register2").click(function () {var IMG_BASE = $("#Image1").attr("src"); //要上传的图片的base64编码
var IMG_ROUTE = $(".imgfile").val();//获取上传图片路径,为获取图片类型使用
var IMG_ENDFOUR = IMG_ROUTE.substr(IMG_ROUTE.length - 4, 4);//截取路径后四位,判断图片类型
var IMG_FOMATE = "jpeg"; //图片类型***
if (IMG_ENDFOUR.trim() == ".jpg")
IMG_FOMATE = "jpg";
else if (IMG_ENDFOUR.trim() == ".png")
IMG_FOMATE = "png";
else if (IMG_ENDFOUR.trim() == ".bmp")
IMG_FOMATE = "bmp";
//图片正式开始上传
$.ajax({
type: "post",
url: "server/head.ashx",
data: { 'imgBase': IMG_BASE, 'imgFormat': IMG_FOMATE },
dataType: "text",
success: function (data) {
//data为返回的图片路径
});
});

一般处理程序部分:

using System.IO; //需要这三个命名空间
using System.Drawing;//***
using System.Drawing.Imaging;//*** string imgBase = context.Request["imgBase"];//传递过来的base64编码
string imgFomate = context.Request["imgFormat"];//传递过来的图片格式
string end = "1";
string imgReadyBase = imgBase.Substring(imgBase.IndexOf("4") + 2);//截取base64编码无用开头
byte[] bytes = System.Convert.FromBase64String(imgReadyBase);//base64转为byte数组
MemoryStream ms = new MemoryStream(bytes);//创建内存流,将图片编码导入
Image img = Image.FromStream(ms);//将流中的图片转换为Image图片对象
//利用时间种子解决伪随机数短时间重复问题
Random ran = new Random((int)DateTime.Now.Ticks);
//文件保存位置及命名,精确到毫秒并附带一组随机数,防止文件重名,数据库保存路径为此变量
string s = ran.Next().ToString();
string serverPath = "../head/" + DateTime.Now.ToString("yyyyMMddhhmmssms") +s+ "." + imgFomate;
//路径映射为绝对路径
string path = context.Server.MapPath(serverPath);
ImageFormat imgfor = ImageFormat.Jpeg;//设置图片格式
if (imgFomate == "png") imgfor = ImageFormat.Png;
try
{
img.Save(path, imgfor);//图片保存
}
catch { end = "0"; }
end = "head/" + DateTime.Now.ToString("yyyyMMddhhmmssms") +s+ "." + imgFomate;
context.Response.Write(end);
context.Response.End();

JQuery input file 上传图片的更多相关文章

  1. jquery解决file上传图片+图片预览

    js解决file上传图片+图片预览 demo案例中代码为js原生控制,可以根据项目的需求修改为jquery操作 <!DOCTYPE html><html lang="en& ...

  2. jQuery input -> file change事件bug

    由jQuery绑定类型为file的input控件的change事件,发现只能被触发一次,修改方法 --> 原始代码: $input.change(function() { // somethin ...

  3. input file 上传图片问题

    html代码如下: <input id="fileup" type="file" accept="image/*" capture=& ...

  4. jquery html5 file 上传图片显示图片

    jquery js 的代码:不同浏览器下的路径 //建立一個可存取到該file的url function getObjectURL(file) {     var url = null ;     i ...

  5. html5手机 input file 上传图片 调用API

    <input type="file" accept="video/*;capture=camcorder"> <input type=&quo ...

  6. (转)html中使用表单和input file上传图片

    本文转载自:http://hi.baidu.com/love_1210/item/120e452b42b2a854c38d59eb 客户端代码: <form name="form1&q ...

  7. input file上传图片预览,非插件

    Input标签 <input type="file" name="pic" onchange="changepic(this)" mu ...

  8. jquery input file 多图上传,单张删除,查看

    <div class="form-group"> <label for="imgs" class="col-md-3 col-sm- ...

  9. 从重置input file标签中看jQuery的 .val() 和 .attr(“value”) 区别

    背景: 在清空input file标签选中值时,分别用了以下方法,发现有的对有的错: [√]$("#file")[0].value = ""; [√]$(&qu ...

随机推荐

  1. python 列表字符串元素乱序

    from random import shuffle color = ['] shuffle(color) print(color)

  2. <<网络是怎样连接的>>笔记第一章browser生成message

    网络:由负责搬运数字信息的机制 + 浏览器和服务器这些网络应用程序. web->协议栈,网卡->交换机,路由器->接入网,网络运营商->(防火墙,缓存服务器)->web服 ...

  3. PHP--------解决网址URL编码问题

    在PHP中有urlencode().urldecode().rawurlencode().rawurldecode()这些函数来解决网页URL编码解码问题. 理解urlencode: urlencod ...

  4. linux三尖刀

    序 我们都知道,一个可执行程序的基本的生命过程是如此的: (编辑)源文件--->(编译)目标文件--->(链接)可执行文件--->(调试排错)稳定执行 所以,在这个过程中,我们很容易 ...

  5. NYOJ 720 DP+二分

    项目安排 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 小明每天都在开源社区上做项目,假设每天他都有很多项目可以选,其中每个项目都有一个开始时间和截止时间,假设做完每个 ...

  6. MyBatise代码自动生成时候Oralce的number类型BigDecimal问题

    使用MyBatise的代码自动生成工具时候,即便在配置文件中定义了 <javaTypeResolver> <property name="forceBigDecimals& ...

  7. 关闭定时器(setTimeout/clearTimeout|setInterval/clearInterval)

    1.1 开启Timeout程序: scope.setTimeout("functionName()" | functionHandle, timeValue) 返回值:timerI ...

  8. BZOJ3239 Discrete Logging

    一道裸的BSGS题目(叫baby step, giant step) 从爱酱的blog里学来的,是一个很神的根号算法. 如果我们有hash的工具的话,就是O(sqrt(p))的,这里又用了一个map所 ...

  9. Nginx 设置rewrite规则是遇到的一个{}大括号引发的报错问题

    一个群友提到: 用nginx image_filter模块裁图,用!拼宽高能够实现,现在想用参数传宽高总是报错,配置如下:   location ~ ^/images/.* {     if ( $q ...

  10. Linux系列:Linux中如何安装.rpm、.tar、.tar.gz和tar.bz2文件

    转载自:https://blog.csdn.net/lanxuezaipiao/article/details/21896579 我以下面三个包为例:(三个包都在/etc/opt下) A. examp ...