HTML5上传图片到ASP.NET.MVC

@{
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();
}
}
}

时间: 2025-01-15 00:24:50

HTML5上传图片到ASP.NET.MVC的相关文章

HTML5 上传图片 到ASP.NET MVC

1 @{ 2 ViewBag.Title = "Home Page"; 3 } 4 5 6 <!DOCTYPE HTML PUBLIC> 7 <html> 8 <head> 9 <meta charset="utf-8"> 10 <script src="//code.jquery.com/jquery-1.11.0.min.js"></script> 11 <titl

无刷新上传图片(asp.net mvc)

步骤: 1,先引用脚本,ajaxfileupload.js和配套的jquery(注意:这里如果版本不同可能会出错) 2,客户端部分代码: <div class="jumbotron"><h1>ASP.NET</h1><input type="file" name="image" id="image" style="width:350px;height:25px;"

ASP.NET MVC下使用文件上传

这里我通过使用uploadify组件来实现异步无刷新多文件上传功能. 1.首先下载组件包uploadify,我这里使用的版本是3.1 2.下载后解压,将组件包拷贝到MVC项目中 3.  根目录下添加新文件夹Uploads,然后新建控制器UploadifyController.cs using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using S

带进度条的上传(Asp.net+MVC+Uploadify)

1.Uploadify简介 Uploadify是基于jQuery的一种上传插件,支持多文件.带进度条显示上传,在项目开发中常被使用. Uploadify官方网址:http://www.uploadify.com/ 2.ASP.NET MVC3中的使用Uploadify 搭建ASP.NET MVC3解决方案如下图,其中使用到的Uploadify为3.1版本: 1>.简单示例 _Layout.cshtml代码: <!DOCTYPE html> <html> <head>

ASP.NET MVC 4 - 上传图片到数据库

这里演示如何在MVC WEB应用程序如何上传图片到数据库以及如何在WEB页面上显示图片.数据库表对应整个Model类,不单图片数据一个字段,我们从数据表的定义开始: CREATE TABLE [dbo].[Products] ( [ProductID] INT IDENTITY (1, 1) NOT NULL, [Name] NVARCHAR (MAX) NOT NULL, [Description] NVARCHAR (MAX) NOT NULL, [Price] DECIMAL (18, 2

[转]Using the HTML5 and jQuery UI Datepicker Popup Calendar with ASP.NET MVC - Part 4

本文转自:http://www.asp.net/mvc/overview/older-versions/using-the-html5-and-jquery-ui-datepicker-popup-calendar-with-aspnet-mvc/using-the-html5-and-jquery-ui-datepicker-popup-calendar-with-aspnet-mvc-part-4 This tutorial will teach you the basics of how

在ASP.NET MVC下有关上传图片脏数据的解决方案

在"在ASP.NET MVC下实现单个图片上传, 客户端服务端双重限制图片大小和格式, 服务端裁剪图片"中,已经实现了在客户端和服务端限制图片大小和格式,以及在服务端裁剪图片.但还有一个重要的话题是需要面对的,那就是图片脏数据问题. 假设用户添加产品信息,并且上传了图片,可之后用户没有点击页面上的添加按钮,这就导致上传图片成为"脏数据",存在着却一直不会被使用.解决这个问题的大致思路是: ○ 在上传图片的时候,把图片保存到一个临时文件夹,或者叫缓存文件夹○ 当用户真

HTML5中custom data-*特性与asp.net mvc 3 表单验证

在Asp.net MVC Web App中原来我们对表单有验证,需要写这个js与Jquery Validation 插件配合, 回顾一下,看下面的代码: <!DOCTYPE html> <html> <head> <title>LoginWithModel</title> <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.5.1.min.js" typ

ASP.NET MVC 4 (十一) Bundles和显示模式

Bundles用于打包CSS和javascript脚本文件,优化对它们的组织管理.显示模式则允许我们为不同的设备显示不同的视图. 默认脚本库 在VS创建一个MVC工程,VS会为我们在scripts目录下添加很多脚本库,下面来简单了解下这些脚本库的作用: 脚本文件 说明 jquery-1.7.1.js jquery的基本类库 jquery-ui-1.8.20.js jquery的UI类库,方便我们创建丰富的用户控件,基于jquery基本类库 jquery.mobile-1.1.0.js 用于移动设