MVC异步上传图片到本地/服务器
这两天朋友问我,有没有异步上传图片到本地/服务器这种demo,他有用, 我就想,好吧, 那刚好周末了,整理一套出来。
主要用到的是jquery uploadify 这个juqery的插件 ,可以无刷新,直接后台上传返回地址
下面先看前台的代码:
- @{
- ViewBag.Title = "Demo";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- @section styles{
- <link href="~/Content/uploadify.css" rel="stylesheet" />
- }
- <h2>Demo</h2>
- <h2>实例</h2>
- <div class="form-group">
- <input class="form-control" type="file" id="PickImage" name="PickImage" value="浏览图片" />
- </div>
- <div class="form-group">
- <img class="img-rounded" id="Rimg" width="200" height="200"/>
- </div>
- @section scripts{
- <script src="~/Scripts/jquery.uploadify.min.js"></script>
- <script>
- jQuery(function () {
- $('#PickImage').uploadify({
- 'swf': '/Content/uploadify.swf', 'buttonText': '选择图片并上传',
- 'uploader': '@Url.Action("UploadImage","Demo")',
- 'fileTypeDesc': '图片类型',
- 'fileTypeExts': '*.jpg;*.jpeg;*.png',
- "formData": { "folder": "/product/" },
- onUploadSuccess: function (localFileObject, serverData, receivedResponse) {
- console.log(serverData)
- if (typeof (serverData) == "string")
- serverData = JSON.parse(serverData);
- $("#HeadImgurl").val(serverData.ImagePath);
- $("#Rimg").attr("src", serverData.ImagePath);
- },
- onUploadComplete: function (fileObj) {
- }
- });
- });
- </script>
- }
后台的代码也很简单:
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace Demo_UploadImageToServer.Controllers
- {
- public class DemoController : Controller
- {
- // GET: Demo
- public ActionResult Demo()
- {
- return View();
- }
- #region 帮助方法
- //图片异步上传
- public ActionResult UploadImage()
- {
- Response.ContentType = "text/plain";
- Response.Charset = "utf-8";
- HttpPostedFileBase file = Request.Files["Filedata"];
- string path = ConfigurationManager.AppSettings["Domain"].ToString(); //填写服务器域名
- string basePath = "/UploadPic/";
- string uploadPath = Server.MapPath(basePath); //本地路径
- if (file != null)
- {
- if (!Directory.Exists(uploadPath))
- {
- Directory.CreateDirectory(uploadPath);
- }
- string fileName = file.FileName;
- string ext = fileName.Substring(fileName.LastIndexOf("."));
- fileName = DateTime.Now.Ticks + ext;
- file.SaveAs(uploadPath + fileName);
- //服务器上传
- //return Json(new { ImagePath = string.Format("{0}{1}{2}", path, basePath, fileName) });
- //本地上传
- return Json(new { ImagePath = string.Format("{0}{1}", basePath, fileName) });
- }
- else
- {
- return Json(new { error = });
- }
- }
- #endregion
- }
- }
MVC异步上传图片到本地/服务器的更多相关文章
- kindeditor修改图片上传路径-使用webapi上传图片到图片服务器
kindeditor是一个非常好用的富文本编辑器,它的简单使用我就不再介绍了. 在这里我着重介绍一些使用kindeditor修改图片上传路径并通过webapi上传图片到图片服务器的方案. 因为我使用的 ...
- kindeditor扩展粘贴图片功能&修改图片上传路径并通过webapi上传图片到图片服务器
前言 kindeditor是一个非常好用的富文本编辑器,它的简单使用我就不再介绍了. 而kindeditor却对图片的处理不够理想. 本篇博文需要解决的问题有两个: kindeditor扩展粘贴图片功 ...
- kindeditor扩展粘贴截图功能&修改图片上传路径并通过webapi上传图片到图片服务器
前言 kindeditor是一个非常好用的富文本编辑器,它的简单使用我就不再介绍了. 而kindeditor却对图片的处理不够理想. 本篇博文需要解决的问题有两个: kindeditor扩展粘贴图片功 ...
- 使用Ajax异步上传图片的方法(html,javascript,php)
前两天项目中需要用到异步上传图片和显示上传进度的功能,于是找了很多外国的文章,翻山越岭地去遇上各种坑,这里写篇文章记录一下. HTML <form id="fileupload-for ...
- Ajax实现异步上传图片
要求:点击页面浏览按钮后,选择需要上传的图片,页面无刷新,将上传的图片展示出来 开发流程 一:在页面编写表单代码和js代码 <!DOCTYPE html PUBLIC "-//W3C/ ...
- 使用html5 FileReader获取图片,并异步上传到服务器(不使用iframe)
使用html5 FileReader获取图片,并异步上传到服务器(不使用iframe) 原理: 1.使用FileReader 读取图片的base64编码 2.使用ajax,把图片的base64编码 ...
- [Ajax] 使用Ajax异步上传图片文件(非Form表单提交)
通过表单Form提交来上传文件的方式这里就不说了: 下面介绍,通过js中使用ajax异步上传图片文件: 新建一个html页面和一个一般处理程序即可: 涉及思路: //发送2次Ajax请求完成js异步上 ...
- mvc异步表单遇到的问题
1,mvc异步表单遇到的问题 问题:使用jqury easy ui 时提交异步数据不能请求到服务器 解决办法:经过细心调试和检测,发现jqury的加载顺序放在了easy ui之后,所以首先加 ...
- asp.net mvc异步查询
对于asp.net mvc异步查询 如何做MVC异步查询,做列表页面. 查询是项目中必不可少的工作,而且不同的项目不同的团队,都有自己的简单方法.Asp.net mvc 有自己独特的优势,下面是结合m ...
随机推荐
- linux常用命令--ubuntu
linux 操作系统 一.linux 操作系统概述 二.安装linux系统 三.linux系统环境 ubuntu,默认有6个命令交互通道和一个图形界面交互通道,默认进入到的是图形界面通道 命令交互模式 ...
- (2)搜索广告CTR预估
https://www.cnblogs.com/futurehau/p/6184585.html 1. CTR预估的流程 数据 -> 预处理 ->特征抽取 ->模型训练 ->后 ...
- Luogu P2970 [USACO09DEC]自私的放牧
https://www.luogu.org/problemnew/show/P2970 P2970 [USACO09DEC]自私的放牧 题目描述 Each of Farmer John's N (1 ...
- 25.TF&IDF算法以及向量空间模型算法
主要知识点: boolean model IF/IDF vector space model 一.boolean model 在es做各种搜索进行打分排序时,会先用boolean mo ...
- file.seek()/tell()-笔记
---------------------------------------------------------------------------------------------------- ...
- 关于PyQt5,在pycharm上的安装步骤及使用技巧
前序 之前学习了一款GUI图形界面设计的Tkinter库,但是经大佬的介绍,PyQT5全宇宙最强,一脸的苦笑 毫不犹豫的选择转战PyQT5,在学习之前需要先安装一些必须程序,在一番查阅后,发现PyQt ...
- Python 6 数字和布尔值及字符串的基本功能
数据类型:查看变量数据类型type(变量) 或者 print(type(变量)) 整数int:就是不带小数的自然数字,也叫整型.在2.X版本中还分为长整型和整形.但是在3.X版本中统一称为整数或整 ...
- [bzoj3192][JLOI2013]删除物品_树状数组_栈
删除物品 bzoj-3192 JLOI-2013 题目大意:给你n个物品,分成2堆.所有的物品有不同的优先级.我只可以将一堆中的堆顶移动到另一个堆的堆顶.而如果当前物品是全局所有物品中优先级最高的,我 ...
- CF #330 D2 E
相当于给你一些点,要你最多删除不超过k,使得能使用一个边长为整数的长方形,与XY轴平行,使长方形的面积最小. 上课时拿笔来画画,然后忽然思路就开了,要是比赛也这样就好了~~先按X,Y分别排序,由于K较 ...
- 【转】】}linux awk 命令详解
http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html ----------------------------------- ...