十六、ajax上传图片 mvc
一、ajax上传图片 mvc
前台html
<img id="uploadimg1" class="uploadimg" src="~/Content/img/sctp.png" width="60" height="60" />
<input type="file" id="inputimg" name="img_file" style="display:none">
<div id="uploadimg" class="uploadPhoto" onclick="uploadPhoto()">
+
</div>
js 上传成功返回url给img
function uploadPhoto() {
document.querySelector('#inputimg').value = null;
$("#inputimg").click();
}
document.getElementById("inputimg").addEventListener("change", function (e) {
var formData = new FormData();
formData.append("file1", document.getElementById("inputimg").files[0]);
$.ajax({
url: "/PersonalCenter/UpLoadProcess",
type: 'POST',
data: formData,
async: false,
contentType: false,
processData: false,
success: function (msg) {
if (msg.error == 0) {
$("#uploadimg1").attr("src", msg.message);
$("#uploadimg1").attr("data-imgurl", msg.message);
}
},
error: function (msg) {
alert(msg.error);
}
});
})
c# mvc后台接收
/// <summary>
/// 上传图片
/// </summary>
public ActionResult UpLoadProcess(HttpPostedFileBase imgFile)
{ Hashtable hash = new Hashtable();
if (Request.Files.Count <= )
{
hash = new Hashtable();
hash["error"] = ;
hash["message"] = "请选择文件";
return Json(hash);
}
imgFile = Request.Files[];
string fileTypes = "gif,jpg,jpeg,png,bmp";
int maxSize = * * ;
string fileName = imgFile.FileName;
string fileExt = Path.GetExtension(fileName).ToLower();
if (imgFile.InputStream == null || imgFile.ContentLength > maxSize) //file.InputStream.Length > maxSize ||
{
hash = new Hashtable();
hash["error"] = ;
hash["message"] = "上传文件大小超过限制";
return Json(hash);
} if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(fileTypes.Split(','), fileExt.Substring().ToLower()) == -)
{
hash = new Hashtable();
hash["error"] = ;
hash["message"] = "上传文件扩展名是不允许的扩展名";
return Json(hash);
} string filePathName = string.Empty;
string localPath = Path.Combine(HttpRuntime.AppDomainAppPath, "Resource");
if (Request.Files.Count == )
{
return Json(new { jsonrpc = 2.0, error = new { code = , message = "保存失败" }, id = "id" });
}
string ex = Path.GetExtension(imgFile.FileName);
filePathName = Guid.NewGuid().ToString("N") + ex;
if (!System.IO.Directory.Exists(localPath))
{
System.IO.Directory.CreateDirectory(localPath);
}
imgFile.SaveAs(Path.Combine(localPath, filePathName));
return Json(new
{
error = ,
message = "/Resource" + "/" + filePathName
}); }
十六、ajax上传图片 mvc的更多相关文章
- SpringMVC系列(十六)Spring MVC与Struts2的对比
• Spring MVC 的入口是 Servlet, 而 Struts2 是 Filter• Spring MVC 会稍微比 Struts2 快些. Spring MVC 是基于方法设计, 而 Stu ...
- 第四十六课:MVC和MVVM的开发区别
实现MVC的目的就是为了让M和V相分离.前端的MVC无法做到View和Model的相分离,而MVVM可以. 我们先来看一个用MVC模式开发的经典例子:(一定要深入了解这种开发的思想,而不是看懂代码) ...
- Unity3D学习笔记(二十六):MVC框架下的背包系统(1)
MVC背包 需求: 1.背包格子的装备是可以拖动的 2.装备栏的装备也是可以拖动的 3.当背包格子的装备拖动到装备栏时,如果是装备类型和装备栏类型是一致的能装上 4.背包的装备是按照顺序放在格子中的, ...
- ASP.NET MVC深入浅出系列(持续更新) ORM系列之Entity FrameWork详解(持续更新) 第十六节:语法总结(3)(C#6.0和C#7.0新语法) 第三节:深度剖析各类数据结构(Array、List、Queue、Stack)及线程安全问题和yeild关键字 各种通讯连接方式 设计模式篇 第十二节: 总结Quartz.Net几种部署模式(IIS、Exe、服务部署【借
ASP.NET MVC深入浅出系列(持续更新) 一. ASP.NET体系 从事.Net开发以来,最先接触的Web开发框架是Asp.Net WebForm,该框架高度封装,为了隐藏Http的无状态模 ...
- Web 前端开发人员和设计师必读精华文章【系列二十六】
<Web 前端开发精华文章推荐>2014年第5期(总第26期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...
- ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 try.dot.net 的正确使用姿势 .Net NPOI 根据excel模板导出excel、直接生成excel .Net NPOI 上传excel文件、提交后台获取excel里的数据
ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不 ...
- 进击的Python【第十六章】:Web前端基础之jQuery
进击的Python[第十六章]:Web前端基础之jQuery 一.什么是 jQuery ? jQuery是一个JavaScript函数库. jQuery是一个轻量级的"写的少,做的多&quo ...
- m_Orchestrate learning system---二十六、动态给封装好的控件添加属性
m_Orchestrate learning system---二十六.动态给封装好的控件添加属性 一.总结 一句话总结:比如我现在封装好了ueditor控件,我外部调用这个控件,因为要写数据到数据库 ...
- ASP.NET Core 2.2 : 十六.扒一扒2.2版更新的新路由方案
原文:ASP.NET Core 2.2 : 十六.扒一扒2.2版更新的新路由方案 ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不大 ...
随机推荐
- shell分析nginx access log日志
统计访问最多的ip1. tail -n 10000 xxaccess_log | cut -d " " -f 1 |sort|uniq -c|sort -rn|head -10 | ...
- ltp-ddt genload
under folder tools\genload genload.c "`%s' imposes certain types of compute stres ...
- boost variant
Boost Variant resembles union. You can store values of different types in a boost::variant. 1. #incl ...
- JS中数据结构之栈
1.栈的基本介绍 栈是一种高效的数据结构,因为数据只能在栈顶添加或删除,所以这样的操作很快,而且容易实现. 栈是一种特殊的列表,栈内的元素只能通过列表的一端访问,这一端称为栈顶.栈被称为一种后入先出( ...
- http 换成 https
UPDATE SYS_MENU M SET M.href = ( SELECT CASE WHEN substr(N.href, 0, 5) = 'http:' THEN 'https:'||subs ...
- kafka-server.properties
# Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreement ...
- basic play
[root@wen ~]# w 19:01:27 up 1 day, 7:06, 3 users, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN ...
- 分享几套bootstrap后台模板【TP5版】
分享几套bootstrap后台模板[TP5版],模板来源于网络,需要的拿走.1.AdminLTE 链接: http://pan.baidu.com/s/1o7BXeCM 密码: zfhy 2.Boot ...
- CF 717A Festival Organization——斯特林数+递推求通项+扩域
题目:http://codeforces.com/contest/717/problem/A 是 BJOI2019 勘破神机 的弱化版. 令 \( g[i] \) 表示长为 i .以 1 结尾的方案数 ...
- Key Set
http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1011&cid=594 Key Set Time Limit: 2000 ...