html5使用FileReader上传图片
客户端代码是网上找的,修改为.net代码。
<html>
<head>
<meta charset="utf-8">
<title>使用html5 FileReader获取图片,并异步上传到服务器(not iframe)</title>
<style type="text/css">
body
{
margin: 0px;
background: #f2f2f0;
}
p
{
margin: 0px;
}
.title
{
color: #FFFF00;
background: #000000;
text-align: center;
font-size: 24px;
line-height: 50px;
font-weight: bold;
}
.file
{
position: absolute;
width: 100%;
font-size: 90px;
display: none;
}
.filebtn
{
display: block;
position: relative;
height: 110px;
color: #FFFFFF;
background: #06980e;
font-size: 48px;
line-height: 110px;
text-align: center;
cursor: pointer;
border: 3px solid #cccccc;
}
.filebtn:hover
{
background: #04bc0d;
}
.showimg
{
margin: 10px auto 10px auto;
text-align: center;
}
</style>
<script type="text/javascript">
window.onload = function () {
// 选择图片
document.getElementById('img').onchange = function () {
var input = document.getElementById("img");
var img = input.files[0];
// 判断是否图片
if (!img) {
return;
}
// 判断图片格式
if (!(img.type.indexOf('image') == 0 && img.type && /\.(?:jpg|png|gif)$/.test(img.name))) {
alert('图片只能是jpg,gif,png');
return;
}
var reader = new FileReader();
reader.readAsDataURL(img);
reader.onload = function (e) { // reader onload start
// ajax 上传图片
$.post("Handler.ashx", { imgname: img.name, img: e.target.result }, function (ret) {
var www = ret;
if (ret != '') {
alert('upload success');
$('#showimg').html('<img src="' + ret + '">');
} else {
alert('upload fail');
}
}, 'text'); //这里返回的类型有:json,html,xml,text
} // reader onload end
}
}
</script>
</head>
<body>
<p class="title">
使用html5 FileReader获取图片,并异步上传到服务器(not iframe)</p>
<p>
<input type="file" class="file" id="img"><%--加入multiple可多选--%><label class="filebtn"
for="img" title="JPG,GIF,PNG">请选择图片</label></p>
<p class="showimg" id="showimg">
</p>
</body>
</html>
服务端代码:
public void ProcessRequest(HttpContext context)
{
if (context.Request["img"] != null)//生成校验码
{
string imgname = context.Request["imgname"];
string imgExtention = System.IO.Path.GetExtension(imgname).ToLower();
if (imgExtention != ".jpg" && imgExtention != ".jpe" && imgExtention != ".jpeg" && imgExtention != ".gif" && imgExtention != ".png" && imgExtention != ".bmp")
{
string s = "原图片文件格式不正确,支持的格式有[ .jpg|.jpe|.jpeg|.png|.bmp|.gif ]!";
}
string imgData = context.Request["img"];
string[] ss =imgData.Split(',');
byte[] imageBytes = Convert.FromBase64String(ss[1]);
//读入MemoryStream对象
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(imageBytes, 0, imageBytes.Length);
memoryStream.Write(imageBytes, 0, imageBytes.Length);
//转成图片
System.Drawing.Image image = System.Drawing.Image.FromStream(memoryStream);
image.Save("硬盘存储地址" + imgname);
context.Response.Write("Web服务器地址" + imgname);
context.Response.End();
}
}
html5使用FileReader上传图片的更多相关文章
- 网站开发进阶(三十二)HTML5之FileReader的使用
HTML5之FileReader的使用 HTML5定义了FileReader作为文件API的重要成员用于读取文件,根据W3C的定义,FileReader接口提供了读取文件的方法和包含读取结果的事件模型 ...
- HTML5 之 FileReader 方法上传并读取文件
原文地址:https://caochangkui.github.io/file-upload/ HTML5 的 FileReader 对象允许Web应用程序异步读取存储在用户计算机上的文件(或原始数据 ...
- HTML5之FileReader的使用
HTML5定义了FileReader作为文件API的重要成员用于读取文件,根据W3C的定义,FileReader接口提供了读取文件的方法和包含读取结果的事件模型. FileReader的使用方式非常简 ...
- HTML5 通过 FileReader 实现文件上传
概述 在页面中上传时,之前一般都是需要使用form表单进行上传.html5 中提供了FileReader 可以将文件转换成Base64编码字符串,因此就可以直接使用 AJAX实现文件上传. 实现代码 ...
- [转] HTML5之FileReader的使用
HTML5定义了FileReader作为文件API的重要成员用于读取文件,根据W3C的定义,FileReader接口提供了读取文件的方法和包含读取结果的事件模型. FileReader的使用方式非常简 ...
- HTML5 本地裁剪上传图片
很多情况下用户上传的图片都需要经过裁剪,比如头像啊什么的.但以前实现这类需求都很复杂,往往需要先把图片上传到服务器,然后返回给用户,让用户确定裁剪坐标,发送给服务器,服务器裁剪完再返回给用户,来回需要 ...
- HTML5之FileReader的使用.RP
HTML5定义了FileReader作为文件API的重要成员用于读取文件,根据W3C的定义,FileReader接口提供了读取文件的方法和包含读取结果的事件模型. FileReader的使用方式非常简 ...
- HTML5 之 FileReader 的使用 (网页上图片拖拽并且预显示可在这里学到) [转载]
转载至 : http://www.360doc.com/content/14/0214/18/1457948_352511416.shtml FileReader 资料(英文) : https://d ...
- FileReader上传图片
实现拖拽图片,在上传至服务器前,显示图片并操控大小 利用HTML5 dragenter dragover dragleave drop 在实现图片显示方面,用了FileReader这个类 var fi ...
随机推荐
- android中如何发送及接收数据(两种方法)?
1.如在MainActivity.java中的按钮点击时设置: //发送数据方法1--简单型 i.putExtra("txt", "没错,我就是刚传来的信息!" ...
- Frament 方法
public final boolean isAdded() 如果该Fragment对象被添加到了它的Activity中,那么它返回true,否则返回false. public final boole ...
- string.Format , object[] args 使用
string sql = "insert into TableA values('{0}','{1}','{2}',GetDate(),'{3}' "; sql = string. ...
- sql行列转换
首先我们建立一张表,名为scoreInfo,各个字段的设计如下图,分别是name,course,score,表示姓名,成绩与分数,如图所示.
- js-特效部分学习-拖拽效果
一.客户区大小ClientWidth和ClientHeight <style> #box { width: 200px; height: 200px; background-color: ...
- Win7 64位 VS2013环境编译boost1_58_0
备忘,发现好多不常用的东西不记笔记再想用要重新花时间找,所以试着开始记笔记,写入博客吧. 首先去官网下最新的版本 http://www.boost.org/ 写本文时boost最新版本为1_58_0, ...
- Apache 常用伪静态配置
1. /a/b?c=d => index.php?_a=a&_m=b&c=d 2. /xxx/detail-yyy.html => index.php?_a=xxx& ...
- cocos2dx release note
[传送门] 发布说明: https://github.com/fusijie/Cocos2dx-ReleaseNote-zh/blob/master/SUMMARY.md 更新记录: https:// ...
- Unrecognized Windows Sockets error: 0: JVM_Bind 异常怎么办
Unrecognized Windows Sockets error: 0: JVM_Bind 异常解决办法 java.net.SocketException: Unrecognized Window ...
- Dynamic CRM 2013学习笔记(四十)流程3 - 对话(Dialog)用法图解
我们将用对话来实现一个简单的满意度调查,下一个问题依赖于上一个问题.对话是同步的,不同于工作流既可以是同步也可以是异步的:对话可以跟用户互动:对话只能手动开始:对话只支持 .Net Framework ...