1. //后台代码
  2. [HttpPost]
  3. public ActionResult CreateCategory(HttpPostedFileBase file)
  4. {
  5. string url = Upload(file);
  6. if(url== "empty")
  7. {
  8. return Json("{state:fault}");
  9. }
  10. factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
  11. var proxy = factory.CreateChannel();
  12. //用户组请求类
  13. ServicePostRequest<Guoany_Ga_B2BGCategory_InsertOrUpdate_Request> request = new ServicePostRequest<Guoany_Ga_B2BGCategory_InsertOrUpdate_Request>();
  14. request.ExtraData = new Guoany_Ga_B2BGCategory_InsertOrUpdate_Request();
  15. request.ExtraData.Parent_Id = "";
  16. request.ExtraData.Creater = "";
  17. request.ExtraData.Creattime = DateTime.Now;
  18. request.ExtraData.Des = "";
  19. request.ExtraData.Category_Id = Guid.NewGuid().ToString();
  20. request.ExtraData.Category_Name = "";
  21. request.ExtraData.Category_Url = url;
  22. request.ExtraData.Is_del = false;
  23. request.ExtraData.Is_show = true;
  24. request.ExtraData.Is_Special = false;
  25. request.ExtraData.Sort_No = 1;
  26. ServicePostResponse<Guoany_Ga_B2BGCategory_InsertOrUpdate_Response> response = proxy.InsertOrUpdateB2B_G_Category(request);
  27. // ViewData["Group_Response"] = response.Data;
  28. bool result = response.Success;
  29.  
  30. return Json("{state:success}");
  31. }
  32. public string Upload(HttpPostedFileBase file)
  33. {
  34. if (file == null)
  35. {
  36. return "empty";
  37. }
  38. var fileName = Path.Combine(Request.MapPath("~/Upload"), Path.GetFileName(file.FileName));
  39.  
  40. if (Directory.Exists(Server.MapPath("~/Upload")) == false)//如果不存在就创建file文件夹
  41. {
  42. Directory.CreateDirectory(Server.MapPath("~/Upload"));
  43. }
  44. file.SaveAs(fileName);
  45. return "upload/"+Path.GetFileName(file.FileName);
  46. }
  47. //-----------------------------------------------------------------------------------------
  48. //前台代码
  49.  
  50. @{
  51. ViewBag.Title = "类别列表";
  52. }
  53.  
  54. <script type="text/javascript">
  55. $(function () {
  56. var pager = $('#dg').datagrid().datagrid('getPager'); // get the pager of datagrid
  57. pager.pagination({
  58. buttons: [{
  59. iconCls: 'icon-search',
  60. handler: function () {
  61. $('#dg').datagrid("reload");
  62. }
  63. }, {
  64. iconCls: 'icon-add',
  65. handler: function () {
  66. alert('add');
  67. }
  68. }, {
  69. iconCls: 'icon-edit',
  70. handler: function () {
  71. alert('edit');
  72. },
  73. iconCls: 'icon-remove',
  74. handler: function () {
  75. alert('删除');
  76. }
  77. }]
  78. });
  79.  
  80. })
  81. function searchCategory() {
  82. var stopOrenable = $('#stopOrenable').combobox('getValue');
  83. var txtCategoryName = $("#txtCategoryName").val();
  84. var showOrNot = $("#showOrNot").is(":checked") == true ? 1 : 0;
  85. var specilCategory = $("#specilCategory").is(":checked") == true ? 1 : 0;
  86.  
  87. $('#dg').datagrid('load', {
  88. queryParams: {
  89. stopOrenable: stopOrenable,
  90. txtCategoryName: txtCategoryName,
  91. showOrNot: showOrNot,
  92. specilCategory: specilCategory
  93.  
  94. }
  95.  
  96. });
  97. //var randnum = Math.floor(Math.random() * 1000000);
  98. //alert(randnum)
  99. //var urls = '/Category_Manage/GetCategoryJsonList?randnum=' + randnum;
  100.  
  101. }
  102. function addCategory() {
  103. $('#dd').dialog('open');
  104.  
  105. }
  106.  
  107. function editCategory() { }
  108.  
  109. function showCategory() { }
  110.  
  111. function hidenCategory() { }
  112.  
  113. function enabledCategory() { }
  114.  
  115. function stopCategory() { }
  116. </script>
  117.  
  118. <script type="text/javascript">
  119. function PreviewImage(imgFile) {
  120. var filextension = imgFile.value.substring(imgFile.value.lastIndexOf("."), imgFile.value.length);
  121. filextension = filextension.toLowerCase();
  122. if ((filextension != '.jpg') && (filextension != '.gif') && (filextension != '.jpeg') && (filextension != '.png') && (filextension != '.bmp')) {
  123. alert("对不起,系统仅支持标准格式的照片,请您调整格式后重新上传,谢谢 !");
  124. imgFile.focus();
  125. }
  126. else {
  127. var path;
  128. if (document.all)//IE
  129. {
  130. imgFile.select();
  131. path = document.selection.createRange().text;
  132.  
  133. document.getElementById("imgPreview").innerHTML = "";
  134. document.getElementById("imgPreview").style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='scale',src=\"" + path + "\")";//使用滤镜效果
  135. }
  136. else//FF
  137. {
  138. path = window.URL.createObjectURL(imgFile.files[0]);
  139. var i = 0;
  140. i++;
  141. $("#imgPreview").append("<img id='img" + i + "' width='120px' height='100px' src='" + path + "'/>");
  142.  
  143. }
  144. }
  145. }
  146. function submitContent()
  147. {
  148. var formData = new FormData();
  149. var file = document.getElementById("upload").files[0];
  150. formData.append("file", file);
  151. $.ajax({
  152. type: "POST",
  153. url: '@Url.Action("CreateCategory", "Category_Manage")',
  154. data: formData,
  155. dataType: 'json',
  156. contentType: false,
  157. processData: false,
  158. success: function (response) {
  159. alert("sucess");
  160. },
  161. error: function (error) {
  162. alert("fault");
  163. }
  164. });
  165. }
  166.  
  167. </script>
  168. <table id="dg" class="easyui-datagrid" title="商品类别列表" style="width:100%;height:250px"
  169. data-options="rownumbers:true,singleSelect:true,url:'/Category_Manage/GetCategoryJsonList',method:'get',toolbar:'#tb',footer:'#ft',pagination:true">
  170. <thead>
  171. <tr>
  172. <th data-options="field:'ParentCategoryName',width:80">上级类目</th>
  173. <th data-options="field:'Category_Name',width:100">类目名称</th>
  174. <th data-options="field:'Is_Del',width:80,align:'right'">启用状态</th>
  175. <th data-options="field:'Des',width:80,align:'right'">描述</th>
  176. <th data-options="field:'Is_Special',width:240">是否特殊项目</th>
  177. <th data-options="field:'Is_show',width:60,align:'center'">展示状态</th>
  178. <th data-options="field:'Sort_No',width:60,align:'center'">排序</th>
  179.  
  180. </tr>
  181. </thead>
  182. </table>
  183. <div id="tb" style="padding:2px 5px;">
  184. <div>
  185. <input type="button" value="新增" onclick="addCategory()" />
  186. <input type="button" value="编辑" onclick="editCategory()" />
  187. <input type="button" value="展示" onclick="showCategory()" />
  188. <input type="button" value="隐藏" onclick="hidenCategory()" />
  189. <input type="button" value="启用" onclick="enabledCategory()" />
  190. <input type="button" value="停用" onclick="stopCategory()" />
  191. </div>
  192. 类目名称 <input id="txtCategoryName" style="width:110px">
  193. 是否停用
  194. <select id="stopOrenable" class="easyui-combobox" panelHeight="auto" style="width:100px">
  195. <option value="1">是</option>
  196. <option value="0">否</option>
  197. </select>
  198. <input type="checkbox" id="specilCategory" /> 特殊类目 <input type="checkbox" id="showOrNot" /> 是否展示
  199. <a href="#" id="search" class="easyui-linkbutton" onclick="searchCategory()" iconCls="icon-search">搜索</a>
  200. </div>
  201. <div id="dd" class="easyui-dialog" title="新增/编辑"
  202. data-options="iconCls:'icon-save',closed:true,modal: true,cache: false" style="width:800px;height:600px;padding:10px">
  203. @using (Html.BeginForm("CreateCategory", "Category_Manage", FormMethod.Post, new { enctype = "multipart/form-data" }))
  204. {
  205. <table style="border:1px solid ;width:700px;height:500px;">
  206. <tr>
  207. <td>父类目:<input type="text" /></td>
  208. <td colspan="2">
  209. 图片上传:<input type="file" name="file" style="width: 200px; height: 20px;" onchange="PreviewImage(this)" id="upload" />
  210. <div id="imgPreview" style="width:120px; height:100px;margin-left: 280px;"></div>
  211.  
  212. </td>
  213.  
  214. </tr>
  215. <tr><td>类目:<input type="text" /></tr>
  216. <tr><td>是否特殊类目:<input type="checkbox" /></td><td>是否显示:<input type="checkbox" /></td></tr>
  217. <tr><td rowspan="1">类描述<textarea rows="2"></textarea></td></tr>
  218. <tr><td rowspan="1"><input onclick="submitContent()" value="提交" /></td></tr>
  219. </table>
  220.  
  221. }
  222. </div>

  

asp.net mvc ajax FileUpload的更多相关文章

  1. ASP.NET MVC Ajax.ActionLink 简单用法

    ASP.NET MVC 项目中,如何使用类似于 iframe 的效果呢?或者说 Ajax 局部刷新,比如下面操作: 我们想要的效果是,点击 About 链接,页面不刷新(地址栏不变),然后下面的内容进 ...

  2. Asp.Net MVC ajax调用 .net 类库问题

    如果你还在为 ajax 调用 .net 类库还束手无策的话,相信这篇博客将帮助你解决这个世纪问题! 因为Visual Studio 内置了asp.net mvc ,不过当你添加asp.net mvc项 ...

  3. Asp.Net MVC Ajax

    将ASP.NET MVC中的form提交改为ajax提交 在ASP.NET MVC视图中通过 @using (Html.BeginForm()) 产生的是form表单提交代码,可以用javascrip ...

  4. ASP.NET MVC Ajax 伪造请求

    1.前言 CSRF(Cross-site request forgery)跨站请求伪造,ASP.NET MVC 应用通过使用AJAX请求来提升用户体验,浏览器开发者工具可以一览众山小,就很容易伪造了请 ...

  5. ASP.NET MVC ajax处理 AjaxResult

    1.统一ASPNET MVC 对ajax请求响应格式定义,方便前端统一处理ajax结果. 1)定义程序返回结果数据格式 /// <summary> /// 执行结果 /// </su ...

  6. ASP.NET MVC AJAX 请求中加入 antiforgerytoken 解决“所需的防伪表单字段“__RequestVerificationToken”不存在”问题

    在ASP.NET mvc中如果在表中使用了@Html.AntiForgeryToken(),ajax post不会请求成功 解决方法是在ajax中加入__RequestVerificationToke ...

  7. ASP.NET MVC AJAX的调用示例

    @{ ViewBag.Title = "Home Page"; //下面引用Jquery和unobtrusive-ajax } @Scripts.Render("~/bu ...

  8. asp.net mvc ajax 异步刷新例子

    这几天在asp.net中使用ajax来做异步刷新,这里整理一下 1.首先看前台页面点击的时候调用函数 function shuxin() { $.ajax( { url: "GetValue ...

  9. asp.net MVC  Ajax.BeginForm 异步上传图片的问题

    当debug到这里,你们就发现不管是 Request.Files["Upload"]亦或 Request.Files[0] 都不会取到文件流. 这就是我要说的,当使用Ajax.Be ...

随机推荐

  1. Android公共技术收集

    UML类图详解 详解Android主流框架不可或缺的基石(ClassLoader,泛型,反射,注解) 公共技术点之  Java注解Annotation 公共技术点之  Java反射Reflection ...

  2. 图解TCP-IP协议

    本文通过图来梳理TCP-IP协议相关知识.TCP通信过程包括三个步骤:建立TCP连接通道,传输数据,断开TCP连接通道.如图1所示,给出了TCP通信过程的示意图. 图1 TCP 三次握手四次挥手 图1 ...

  3. [CodeIgniter] 在自定义类库中使用config配置项

    通常情况下,Controller 中的方法可以通过 $this->config->item('item_name') 的方式来加载配置文件中的值 但是如果不继承 CI_Controller ...

  4. ContextFlyout 在10586或10240的使用

    虽然ContextFlyout只能在红石以上版本使用,但可以采用附加属性的方法手动写一个 public static class ContextFlyoutSetter { public static ...

  5. Mysql 如何批量插入百万行测试数据

    Mysql 如何批量插入百万行测试数据

  6. Zabbix协议分析

    概述 Zabbix使用一种自定义的基于TCP的协议与客户端进行通信 Zabbix <- TCP -> Zabbix agent 协议内容 <HEADER> - bytes) & ...

  7. EventDispatcher 事件分发组件

    引言 考虑这样一个问题,现在你想给为你的项目提供一个插件系统,插件可以添加一些方法,或者在某些方法执行之前或者之后做些事情,而不干扰其他插件.要实现这个系统,简单的单继承不是个好办法,即使多继承在PH ...

  8. C#微信公众平台接入示例代码

    http://mp.weixin.qq.com/wiki/17/2d4265491f12608cd170a95559800f2d.html 这是微信公众平台提供的接入指南.官网只提供了php的示例代码 ...

  9. Web Service简要概念,学习记录!

    Web Service平台需要一套协议来实现分布式应用程序的创建.任何平台都有它的数据表示方法和类型系统.要实现互操作性,Web Service平台必须提供一套标准的类型系统,用于沟通不同平台.编程语 ...

  10. Css3阴影实例

    阴影大约从CSS2就开始有了,但是只有Safari一个浏览器支持它,到现在依然是这样.阴影在CSS3中可以应用在边框和文字上,就像图片的阴影效果一样.一般可以分为: box-shadow textsh ...