megapix-image插件 使用Canvas压缩图片上传
2020-12-13 05:21
标签:style blog http java color 使用 os strong MegaPixImage.js 后台对post请求处理: megapix-image插件 使用Canvas压缩图片上传,搜素材,soscw.com megapix-image插件 使用Canvas压缩图片上传 标签:style blog http java color 使用 os strong 原文地址:http://www.cnblogs.com/FH-cnblogs/p/3865156.htmlDOCTYPE html >
html>
head>
title>通过Canvas及File API缩放并上传图片title>
script src="/Scripts/Jquery/jquery-1.8.3.min.js" type="text/javascript">script>
script src="/Scripts/MegaPixImage/MegaPixImage.js" type="text/javascript">script>
script type="text/javascript" language="javascript">
function sendImage() {
// 获取 canvas DOM 对象
var canvas = document.getElementById("myCanvas");
// 获取Base64编码后的图像数据,格式是字符串
// "data:image/png;base64,"开头,需要在客户端或者服务器端将其去掉,后面的部分可以直接写入文件。
var dataurl = canvas.toDataURL("image/png");
// 为安全 对URI进行编码
// data%3Aimage%2Fpng%3Bbase64%2C 开头
var imagedata = encodeURIComponent(dataurl);
//imagedata = imagedata.replace(‘data:image/png;base64,‘, ‘‘);
var url = "/APPCommon/SavePostCanvasImage";
// 因为是string,所以服务器需要对数据进行转码,写文件操作等。
// 个人约定,所有http参数名字全部小写
console.log(dataurl);
console.log(imagedata);
var data = {
imagename: "myImage.png",
imagedata: imagedata
};
jQuery.ajax({
url: url,
data: data,
type: "POST",
// 期待的返回值类型
dataType: "json",
complete: function (xhr, result) {
console.log(xhr.responseText);
var $tip2 = $("#tip2");
if (!xhr) {
$tip2.text(‘网络连接失败!‘);
return false;
}
var text = xhr.responseText;
if (!text) {
$tip2.text(‘网络错误!‘);
return false;
}
var json = eval("(" + text + ")");
if (!json) {
$tip2.text(‘解析错误!‘);
return false;
} else {
$tip2.text(json.Result);
}
}
});
};
script>
head>
body>
div>
input type="file" capture="camera" accept="image/*" id="cameraInput" name="cameraInput"
class="sign_file" />
canvas id="myCanvas">canvas>
button id="btnsend" onclick="sendImage()">上 传button>
div>
label id="tip2">
label>
div>
div>
script type="text/javascript" language="javascript">
var fileInput = document.getElementById(‘cameraInput‘);
fileInput.onchange = function () {
var file = fileInput.files[0];
var mpImg = new MegaPixImage(file);
var resCanvas1 = document.getElementById(‘myCanvas‘);
var _max = 320;
mpImg.render(resCanvas1, {
maxHeight: _max
});
// var resCanvas2 = document.getElementById(‘resultCanvas2‘);
// mpImg.render(resCanvas2, {
// maxWidth: 300,
// maxHeight: 300,
// orientation: 6
// });
};
script>
body>
html>
/**
* Mega pixel image rendering library for iOS6 Safari
*
* Fixes iOS6 Safari‘s image file rendering issue for large size image (over mega-pixel),
* which causes unexpected subsampling when drawing it in canvas.
* By using this library, you can safely render the image with proper stretching.
*
* Copyright (c) 2012 Shinichi Tomita
[HttpPost]
public ActionResult SavePostCanvasImage(string imageData)
{
var ResObj = new ResWithObjstring, UploadImgInfo> { IsResult = false, Result = string.Empty };
try
{
var config = WebConfigs.GetConfig();
string _SavePath = Server.MapPath(config.SavePath);
string fileNameWitPath = _SavePath + DateTime.Now.ToString().Replace("/", "-").Replace(" ", "- ").Replace(":", "") + ".png";
// 去除开头不合理的数据 data:image/png;base64,
imageData = imageData.Substring(30);
string img = HttpUtility.UrlDecode(imageData);
//保存字节流
System.IO.FileStream fs = new System.IO.FileStream(fileNameWitPath, System.IO.FileMode.Create);
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs);
byte[] data = Convert.FromBase64String(img);
bw.Write(data);
bw.Close();
ResObj.IsResult = true;
ResObj.Result = "上传成功";
}
catch (Exception ex)
{
ResObj.IsResult = false;
ResObj.Result = ex.Message;
}
return Content(JsonConvert.SerializeObject(ResObj));
}
上一篇:css圆角矩形属性
文章标题:megapix-image插件 使用Canvas压缩图片上传
文章链接:http://soscw.com/essay/30804.html