<script src="js/lrz.pc.min.js" type="text/javascript"></script> //电脑端
<script src="js/lrz.mobile.min.js" type="text/javascript"></script> //手机端
//js代码
var filename = $("#fileName").val();
var mime = filename.toLowerCase().substr(filename.lastIndexOf("."));
if (mime == ".jpg" || mime == ".png" || mime == ".jpeg" || mime == ".gif") {
var sizes = $("#" + c)[0].files[0].size;
if (sizes > 1024 * 1024 * 5) {
layer.msg("您上传的文件不能大于5M");
return;
}
// loading层
var index = layer.msg('加载中', { icon: 16, time: 30000 });
lrz($("#fileName")[0].files[0], { quality: 0.5, height: 1500 }, function (rst) {
var file = rst.base64;
if (file.indexOf("image") >= 0) {
var fd = new FormData();
fd.append("fileToUpload", file);
fd.append("CallType", "UploadImg");
fd.append("strType", e);
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", uploadComplete, false);
xhr.open("POST", "/ashx/OrderNote.ashx", false);
xhr.send(fd);
}
function uploadComplete(evt) {
var data = evt.target.responseText;
if (data != "") {
……………………
}
layer.close(index);
}
});
} else {
layer.msg("你上传的图片格式不正确,请重新选择!");
return;
}
//一般处理程序OrderNote.ashx
string datetimestring = DateTime.Now.ToString("yyyyMMddhhmmss");
var picbase = HttpContext.Current.Request.Form["fileToUpload"];
if (picbase != null)
{
MemoryStream stream = new MemoryStream(Convert.FromBase64String(picbase.ToString().Replace("data:image/jpeg;base64,", "")));
Bitmap img = new Bitmap(stream);
if (!Directory.Exists(context.Server.MapPath("\assets\UploadImgs")))//如果不存在就创建file文件夹
{
Directory.CreateDirectory(context.Server.MapPath("\assets\UploadImgs"));
}
string imgpath = HttpContext.Current.Server.MapPath("\assets\UploadImgs\");//路径,相对于服务器当前的路径
imgpath = String.Format("{0}{1}.jpg", imgpath, datetimestring);
string XdPath = "assets/UploadImgs/" + datetimestring + ".jpg";
img.Save(imgpath);
}
//另一种上传图片的方式
//HttpPostedFile hpf = HttpContext.Current.Request.Files["fileToUpload"];//HttpPostedFile提供对客户端已上载的单独文件的访问
//if (hpf != null)
//{
// string houzhui = Path.GetExtension(hpf.FileName);
// if (!Directory.Exists(context.Server.MapPath("\assets\UploadImgs")))//如果不存在就创建file文件夹
// {
// Directory.CreateDirectory(context.Server.MapPath("\assets\UploadImgs"));
// }
// string savepath = context.Server.MapPath("\assets\UploadImgs\" + datetimestring + houzhui);//路径,相对于服务器当前的路径
// string PicURL = "assets/UploadImgs/" + datetimestring + houzhui;
// hpf.SaveAs(savepath);//保存