码迷,mamicode.com
首页 > Web开发 > 详细

HTML5上传图片到ASP.NET.MVC

时间:2016-11-10 13:49:55      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:charset   das   reader   head   string   change   ica   font   .sh   

@{
ViewBag.Title = "Home Page";
}


<!DOCTYPE HTML PUBLIC>
<html>
<head>
<meta charset="utf-8">
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<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;
}

.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 (event) {

var img = event.target.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);
console.log(3434);
reader.onload = function (e) { // reader onload start
// ajax 上传图片
$.post("@Url.Content("~/Home/SaveFile")", { img: e.target.result }, function (ret) {

console.log(ret.Path);

alert(ret.Path);
$(‘#showimg‘).html(‘<img src="‘ + ret.Path + ‘">‘);
alert(ret);
}, ‘json‘);
} // reader onload end
}

}
</script>

</head>

<body>
<p class="title">使用html5 FileReader获取图片,并异步上传到服务器(not iframe)</p>
<p><input type="file" class="file" id="img"><label class="filebtn" for="img" title="JPG,GIF,PNG">请选择图片</label></p>

<div style="height:400px;"></div>
<div class="showimg" id="showimg" style="border:solid 1px red;"></div>


</body>
</html>

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

using Html5Image.Tools;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Html5Image.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}

public JsonResult SaveFile( string img)
{
int pos = img.IndexOf("base64,");
if(pos >= 0)
{
img = img.Substring(pos + 7);
}

string file = "UploadedImage\\testimg.jpg";
string path = Path.Combine(HttpRuntime.AppDomainAppPath, file);
ImageTool.SaveFile(img, path, System.Drawing.Imaging.ImageFormat.Jpeg);

var obj = new { Path= Url.Content("~/" + file) };
return Json(obj);
//return "233";
}

public ActionResult About()
{
ViewBag.Message = "Your application description page.";

return View();
}

public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";

return View();
}
}
}

 

HTML5上传图片到ASP.NET.MVC

标签:charset   das   reader   head   string   change   ica   font   .sh   

原文地址:http://www.cnblogs.com/zhang-Y/p/6050316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!