html5 完整图片上传
<div class="photo" style="display:none;" id="upPhoto"><div class="pop-cover"></div><div style="position: absolute; top:30%; left:5%; width:90%; margin:0 auto; z-index:99;">
<a href="#" class="potos tbtm" ><input type="file" name="file" id="file1" style="display:block; width:100%; text-align:center; opacity: 0; cursor:pointer;"/><span style=" z-index:1; position:relative; top:-8px; ">本地上传</span></a><a href="#" class="cancel">取消</a></div></div>
</div>
</section>
</div>
</form>
</body>
<script type="text/javascript" src="js/lib/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="js/lib/ajaxfileupload.js"></script>
<script>
$(document).ready (function () {
$("#head").click(function () {
$(".photo").show();
});
$("#file1").on("change", function () {
change();
})
function change(target) {
$(".photo").hide();
var allowExtention = ".jpg,.bmp,.png,.jpeg";
// console.log(target);
var input = document.getElementById("file1"); //获取input标签
var extention = input.value.substring(input.value.lastIndexOf(".") + 1).toLowerCase(); //截取后缀
if (extention && allowExtention.indexOf(extention) > -1) { //判断上传图片类型
var fileSize = input.files[0].size; //获取上传图片大小
if (fileSize) {
if (fileSize <1.5* 1000 * 1024) { //js限制图片上传大小
ajaxFileUpload();
}
else
addSystemTip($page, "上传图片过大!");
}
}
else
addSystemTip($page, "上传图片格式不正确!");
}
// 使用jquery ajaxFileupload插件 原理是把一个form提交到后端来获取图片
//所以 input type=file 标签必须要给一个 name属性,是必须的否则后台无法获取图片
function ajaxFileUpload() {
$.ajaxFileUpload({
url: "MyAccount.aspx?type=img"////用于文件上传的服务器端请求地址
, secureuri: false // //是否需要安全协议,一般设置为false
, fileElementId: 'file1' //文件上传域的ID
, dataType: 'json' //返回值类型 一般设置为json
// , type: 'post'
, success: function (data, status) //服务器成功响应处理函数
{
$("#img").attr("src", data.imgurl);
// var input = document.getElementById("file1");
var span = $("#file1").next();
$("#file1").remove(); //上传成功后删除file标签,否则无法继续上传
var ele = $('<input type="file" name="file" id="file1" style="display:block; width:100%; text-align:center; opacity: 0; cursor:pointer;"/>'); //重新创建file标签
ele.insertBefore(span);
ele.on("change", function () { //重新绑定change事件句柄
change();
})
$(".photo").hide();
},
error: function (data, status, e)//服务器响应失败处理函数
{
$(".photo").hide();
}
});
}
$(".cancel").click(function () {
$(".photo").hide();
})
$(".pop-cover").mousemove(function () {
$(".photo").hide();
})
})
</script>
</html>
//对应后台代码,并且将上传图片重新绘画一下 指定像素大小
public partial class MyAccount : MemberBase
{
public MemberDetails user = new MemberDetails();
public string ImageURl = "";
public string encodeName = "";
public string sex = "保密";
string domain = XpVShop.MyFuc.Utils.GetAppSettingValue("turnUrl") + "/Upload/users/";
string localUrl = XpVShop.MyFuc.Utils.GetAppSettingValue("PCUploadPath2");
// string domain = "http://localhost:89/Upload/images/";//本地测试使用
// string localUrl=@"F:\hz\code\aishop\Publish\vshop\Upload\images\";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
XpVShop.MyFuc.LogHelper.Write("MyAccount mid:" + CurrentUser.MemberID);
user = MemberBLL.GetMemberDetails(CurrentUser.MemberID);
string u = "vshop_ChangeName.aspx";
encodeName = XpVShop.MyFuc.Utils.UrlEncode(u);
if (user.Photo.IndexOf("http://wx.qlogo.cn") == -1)
ImageURl = domain + user.Photo;
else
ImageURl = user.Photo;
if (!string.IsNullOrEmpty(user.Mobile))
user.Mobile = user.Mobile.Substring(0, 5) + "***" + user.Mobile.Substring(user.Mobile.Length - 1, 1);
;
if (user.Sex) sex = "男";
else sex = "女";
string type = Utils.ReqStrParams("type", "");
if (type == "img")
{
HttpFileCollection files = Request.Files;
string msg = string.Empty;
string error = string.Empty;
string imgurl;
if (files.Count > 0)
{
string name = System.IO.Path.GetFileName(files[0].FileName);
// string url = Server.MapPath("Upload/images/") + CurrentUser.MemberID + name;
XpVShop.MyFuc.LogHelper.Write(" MyAccount 存放路径 localUrl:" + localUrl);
try
{
string strGuid = System.Guid.NewGuid().ToString().ToUpper(); //始化 Guid 类的新
string strFileExt = name.Substring(name.LastIndexOf(".")); //得到图片的后缀
string path = localUrl + strGuid + strFileExt;
System.Web.HttpPostedFile postedFile = files[0];
XpVShop.MyFuc.LogHelper.Write("MyAccount 存放地址之前;" + path);
// files[0].SaveAs(localUrl + CurrentUser.MemberID + name);
XpVShop.MyFuc.Utils.GetThumbNail(name, 350,200, "image/pjpeg", false, postedFile.InputStream, path);
imgurl = strGuid + strFileExt;
user.Photo = imgurl;
MemberBLL.UpdateMemberPhoto(user);
CurrentUser.Photo = imgurl;
ImageURl = domain + user.Photo;
XpVShop.MyFuc.LogHelper.Write(" MyAccount 保存图片路径" + ImageURl + "保存数据库字段:user.Photo:" + imgurl + "存放地址 " + path);
// msg = " 成功! 文件大小为:" + files[0].ContentLength;
string res = "{ error:'" + error + "', msg:'" + msg + "',imgurl:'" + ImageURl + "'}";
Response.Write(res);
Response.End();
}
catch (Exception ex)
{
XpVShop.MyFuc.LogHelper.Write("MyAccount ex:" + ex.ToString());
}
}
}
}
}
}
/// <summary>
/// 处理上传图片为指定像素
/// </summary>
/// <param name="strFileName">文件名</param>
/// <param name="iWidth"></param>
/// <param name="iheight"></param>
/// <param name="strContentType">图片类型</param>
/// <param name="blnGetFromFile">是否是从本地文件名获取文件流</param>
/// <param name="ImgStream">文件流</param>
/// <param name="path">保存路径</param>
public static void GetThumbNail(string strFileName, int iWidth, int iheight, string strContentType, bool blnGetFromFile, System.IO.Stream ImgStream, string path)
{
System.Drawing.Image oImg;
if (blnGetFromFile) oImg = System.Drawing.Image.FromFile(strFileName);
oImg = System.Drawing.Image.FromStream(ImgStream);
oImg = oImg.GetThumbnailImage(iWidth, iheight, null, IntPtr.Zero); //GetThumbnailImage方法是返回此Image对象的缩略图
// string strGuid = System.Guid.NewGuid().ToString().ToUpper(); //始化 Guid 类的新
// string strFileExt = strFileName.Substring(strFileName.LastIndexOf(".")); //得到图片的后缀
//MemoryStream MemStream = new MemoryStream(); //创建其支持存储区为内存的流
oImg.Save(path);
}
html5 完整图片上传的更多相关文章
- MVC4中基于bootstrap和HTML5的图片上传Jquery自定义控件
场景:mvc4中上传图片,批量上传,上传前浏览,操作.图片进度条. 解决:自定义jquery控件 没有解决:非图片上传时,会有浏览样式的问题; 解决方案; 1.样式 – bootstrap 的css和 ...
- iOS 开发之路(WKWebView内嵌HTML5之图片上传) 五
HTML5页面的图片上传功能在iOS端的实现. 首先,页面上用的是plupload组件,在wkwebview上存在两个坑需要修复才能正常使用. 问题:在webview上点击选择照片/相机拍摄,就会出现 ...
- hybird app项目实例:安卓webview中HTML5拍照图片上传
应用的平台环境:安卓webview: 涉及的技术点: (1) <input type="file" > :在开发中,安卓webview默认点击无法调用文件选择与相机拍照 ...
- 【转】HTML5 jQuery图片上传前预览
hTML5实现表单内的上传文件框,上传前预览图片,针刷新预览images,本例子主要是使用HTML5 的File API,建立一個可存取到该 file的url,一个空的img标签,ID为img0,把选 ...
- HTML5 jQuery图片上传前预览
hTML5实现表单内的上传文件框,上传前预览图片,针刷新预览images,本例子主要是使用HTML5 的File API,建立一個可存取到该file的url,一个空的img标签,ID为img0,把选择 ...
- HTML5 之图片上传预处理
在开发 H5 应用的时候碰到一个问题,应用只需要一张小的缩略图,而用户用手机上传的确是一张大图,手机摄像机拍的图片好几 M,这可要浪费很多流量. 像我这么为用户着想的程序员,绝对不会让这种事情发生的, ...
- html图片上传阅览并且点击放大
关闭 qq_31540195的博客 目录视图 摘要视图 订阅 异步赠书:9月重磅新书升级,本本经典 程序员9月书讯 每周荐书: ...
- HTML5笔记:跨域通讯、多线程、本地存储和多图片上传技术
最近做项目在前端我使用了很多新技术,这些技术有bootstrap.angularjs,不过最让我兴奋的还是使用了HTML5的技术,今天我想总结一些HTML5的技术,好记性不如烂笔头,写写文章可以很好的 ...
- LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android
LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android jincon 发表于 2015-02-26 18:31:01 发表在: php开发 localresiz ...
随机推荐
- 关于SqlHelper的详解
SqlHelper是一个基于.NET Framework的数据库操作组件.组件中包含数据库操作方法.SqlHelper用于简化你重复的去写那些数据库连接(SqlConnection),SqlComma ...
- ORM即 对象-关系映射(转自:微冷的雨)
ORM即 对象-关系映射: 将数据库中的数据关系表,映射为实体对象. 灵动思绪EF(Entity FrameWork) 作者: 微冷的雨 来源: 博客园 发布时间: 2013-01-22 16:2 ...
- 使用Graph工具观察FFT波形
2014年8月1日,经过很长时间的上网查阅资料,走了很多弯路,终于可以成功使用Graph工具仿真波形了,虽然这个的确很简单,但是经过自己摸索出来的,兴奋之情难于言表. 明天就是七夕了,刚和女朋友分手的 ...
- [delphi]运行cmd命令,并取得输出字符
http://blog.csdn.net/nerdy/article/details/8969189 [delphi]运行cmd命令,并取得输出字符 标签: delphiCMD命令 2013-05- ...
- machine leanring 笔记 vectorization
the summation of the product of two terms can be expressed as the product of two vectors ps. surf ...
- 团队编程——web应用之人事管理系统
本次作业为团队作业,团队博客要求如下:1. 介绍团队情况:包括队长.成员.队名.成员照片.队训--.等:2. 介绍团队项目名称.总体任务,各成员任务等:3. 每个队做 一次需求调研(针对团队项目),要 ...
- String 与 StringBuffer的区别
String="a" 的方式每相加一次就创建一个新的常量,原常量不消失,比较占内存:StringBuffer是放在堆里面,append直接在原地址相加,不占内存
- navDemo
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- c语言中三个点的解释 : variadic
3.6 Variadic Macros A macro can be declared to accept a variable number of arguments much as a funct ...
- 关于Thomas Brinkhoff移动对象生成器的修改
关于地图数据的写出 控制地图路径数据的输出 修改routing.Edge.java 路径写出源码 public void write (EntryWriter out) { out.print(id) ...