这两天朋友问我,有没有异步上传图片到本地/服务器这种demo,他有用, 我就想,好吧, 那刚好周末了,整理一套出来。

主要用到的是jquery uploadify 这个juqery的插件 ,可以无刷新,直接后台上传返回地址

下面先看前台的代码:

  1. @{
  2. ViewBag.Title = "Demo";
  3. Layout = "~/Views/Shared/_Layout.cshtml";
  4. }
  5.  
  6. @section styles{
  7. <link href="~/Content/uploadify.css" rel="stylesheet" />
  8. }
  9.  
  10. <h2>Demo</h2>
  11.  
  12. <h2>实例</h2>
  13. <div class="form-group">
  14. <input class="form-control" type="file" id="PickImage" name="PickImage" value="浏览图片" />
  15. </div>
  16.  
  17. <div class="form-group">
  18. <img class="img-rounded" id="Rimg" width="200" height="200"/>
  19. </div>
  20.  
  21. @section scripts{
  22. <script src="~/Scripts/jquery.uploadify.min.js"></script>
  23. <script>
  24. jQuery(function () {
  25. $('#PickImage').uploadify({
  26. 'swf': '/Content/uploadify.swf', 'buttonText': '选择图片并上传',
  27. 'uploader': '@Url.Action("UploadImage","Demo")',
  28. 'fileTypeDesc': '图片类型',
  29. 'fileTypeExts': '*.jpg;*.jpeg;*.png',
  30. "formData": { "folder": "/product/" },
  31. onUploadSuccess: function (localFileObject, serverData, receivedResponse) {
  32. console.log(serverData)
  33. if (typeof (serverData) == "string")
  34. serverData = JSON.parse(serverData);
  35. $("#HeadImgurl").val(serverData.ImagePath);
  36. $("#Rimg").attr("src", serverData.ImagePath);
  37. },
  38. onUploadComplete: function (fileObj) {
  39. }
  40. });
  41.  
  42. });
  43. </script>
  44. }

后台的代码也很简单:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Mvc;
  8.  
  9. namespace Demo_UploadImageToServer.Controllers
  10. {
  11. public class DemoController : Controller
  12. {
  13. // GET: Demo
  14. public ActionResult Demo()
  15. {
  16. return View();
  17. }
  18.  
  19. #region 帮助方法
  20. //图片异步上传
  21. public ActionResult UploadImage()
  22. {
  23. Response.ContentType = "text/plain";
  24. Response.Charset = "utf-8";
  25.  
  26. HttpPostedFileBase file = Request.Files["Filedata"];
  27. string path = ConfigurationManager.AppSettings["Domain"].ToString(); //填写服务器域名
  28. string basePath = "/UploadPic/";
  29. string uploadPath = Server.MapPath(basePath); //本地路径
  30. if (file != null)
  31. {
  32. if (!Directory.Exists(uploadPath))
  33. {
  34. Directory.CreateDirectory(uploadPath);
  35. }
  36. string fileName = file.FileName;
  37. string ext = fileName.Substring(fileName.LastIndexOf("."));
  38. fileName = DateTime.Now.Ticks + ext;
  39. file.SaveAs(uploadPath + fileName);
  40.  
  41. //服务器上传
  42. //return Json(new { ImagePath = string.Format("{0}{1}{2}", path, basePath, fileName) });
  43.  
  44. //本地上传
  45. return Json(new { ImagePath = string.Format("{0}{1}", basePath, fileName) });
  46. }
  47. else
  48. {
  49. return Json(new { error = });
  50. }
  51. }
  52. #endregion
  53. }
  54. }

MVC异步上传图片到本地/服务器的更多相关文章

  1. kindeditor修改图片上传路径-使用webapi上传图片到图片服务器

    kindeditor是一个非常好用的富文本编辑器,它的简单使用我就不再介绍了. 在这里我着重介绍一些使用kindeditor修改图片上传路径并通过webapi上传图片到图片服务器的方案. 因为我使用的 ...

  2. kindeditor扩展粘贴图片功能&修改图片上传路径并通过webapi上传图片到图片服务器

    前言 kindeditor是一个非常好用的富文本编辑器,它的简单使用我就不再介绍了. 而kindeditor却对图片的处理不够理想. 本篇博文需要解决的问题有两个: kindeditor扩展粘贴图片功 ...

  3. kindeditor扩展粘贴截图功能&修改图片上传路径并通过webapi上传图片到图片服务器

    前言 kindeditor是一个非常好用的富文本编辑器,它的简单使用我就不再介绍了. 而kindeditor却对图片的处理不够理想. 本篇博文需要解决的问题有两个: kindeditor扩展粘贴图片功 ...

  4. 使用Ajax异步上传图片的方法(html,javascript,php)

    前两天项目中需要用到异步上传图片和显示上传进度的功能,于是找了很多外国的文章,翻山越岭地去遇上各种坑,这里写篇文章记录一下. HTML <form id="fileupload-for ...

  5. Ajax实现异步上传图片

    要求:点击页面浏览按钮后,选择需要上传的图片,页面无刷新,将上传的图片展示出来 开发流程 一:在页面编写表单代码和js代码 <!DOCTYPE html PUBLIC "-//W3C/ ...

  6. 使用html5 FileReader获取图片,并异步上传到服务器(不使用iframe)

    使用html5 FileReader获取图片,并异步上传到服务器(不使用iframe)   原理: 1.使用FileReader 读取图片的base64编码 2.使用ajax,把图片的base64编码 ...

  7. [Ajax] 使用Ajax异步上传图片文件(非Form表单提交)

    通过表单Form提交来上传文件的方式这里就不说了: 下面介绍,通过js中使用ajax异步上传图片文件: 新建一个html页面和一个一般处理程序即可: 涉及思路: //发送2次Ajax请求完成js异步上 ...

  8. mvc异步表单遇到的问题

    1,mvc异步表单遇到的问题    问题:使用jqury easy ui 时提交异步数据不能请求到服务器   解决办法:经过细心调试和检测,发现jqury的加载顺序放在了easy ui之后,所以首先加 ...

  9. asp.net mvc异步查询

    对于asp.net mvc异步查询 如何做MVC异步查询,做列表页面. 查询是项目中必不可少的工作,而且不同的项目不同的团队,都有自己的简单方法.Asp.net mvc 有自己独特的优势,下面是结合m ...

随机推荐

  1. linux常用命令--ubuntu

    linux 操作系统 一.linux 操作系统概述 二.安装linux系统 三.linux系统环境 ubuntu,默认有6个命令交互通道和一个图形界面交互通道,默认进入到的是图形界面通道 命令交互模式 ...

  2. (2)搜索广告CTR预估

    https://www.cnblogs.com/futurehau/p/6184585.html 1. CTR预估的流程 数据 -> 预处理 ->特征抽取 ->模型训练 ->后 ...

  3. Luogu P2970 [USACO09DEC]自私的放牧

    https://www.luogu.org/problemnew/show/P2970 P2970 [USACO09DEC]自私的放牧 题目描述 Each of Farmer John's N (1 ...

  4. 25.TF&IDF算法以及向量空间模型算法

    主要知识点: boolean model IF/IDF vector space model     一.boolean model     在es做各种搜索进行打分排序时,会先用boolean mo ...

  5. file.seek()/tell()-笔记

    ---------------------------------------------------------------------------------------------------- ...

  6. 关于PyQt5,在pycharm上的安装步骤及使用技巧

    前序 之前学习了一款GUI图形界面设计的Tkinter库,但是经大佬的介绍,PyQT5全宇宙最强,一脸的苦笑 毫不犹豫的选择转战PyQT5,在学习之前需要先安装一些必须程序,在一番查阅后,发现PyQt ...

  7. Python 6 数字和布尔值及字符串的基本功能

    数据类型:查看变量数据类型type(变量)  或者  print(type(变量)) 整数int:就是不带小数的自然数字,也叫整型.在2.X版本中还分为长整型和整形.但是在3.X版本中统一称为整数或整 ...

  8. [bzoj3192][JLOI2013]删除物品_树状数组_栈

    删除物品 bzoj-3192 JLOI-2013 题目大意:给你n个物品,分成2堆.所有的物品有不同的优先级.我只可以将一堆中的堆顶移动到另一个堆的堆顶.而如果当前物品是全局所有物品中优先级最高的,我 ...

  9. CF #330 D2 E

    相当于给你一些点,要你最多删除不超过k,使得能使用一个边长为整数的长方形,与XY轴平行,使长方形的面积最小. 上课时拿笔来画画,然后忽然思路就开了,要是比赛也这样就好了~~先按X,Y分别排序,由于K较 ...

  10. 【转】】}linux awk 命令详解

    http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html ----------------------------------- ...