asp.net mvc ajax FileUpload
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | //后台代码 [HttpPost] public ActionResult CreateCategory(HttpPostedFileBase file) { string url = Upload(file); if (url== "empty" ) { return Json( "{state:fault}" ); } factory.Endpoint.Behaviors.Add( new WebHttpBehavior()); var proxy = factory.CreateChannel(); //用户组请求类 ServicePostRequest<Guoany_Ga_B2BGCategory_InsertOrUpdate_Request> request = new ServicePostRequest<Guoany_Ga_B2BGCategory_InsertOrUpdate_Request>(); request.ExtraData = new Guoany_Ga_B2BGCategory_InsertOrUpdate_Request(); request.ExtraData.Parent_Id = "" ; request.ExtraData.Creater = "" ; request.ExtraData.Creattime = DateTime.Now; request.ExtraData.Des = "" ; request.ExtraData.Category_Id = Guid.NewGuid().ToString(); request.ExtraData.Category_Name = "" ; request.ExtraData.Category_Url = url; request.ExtraData.Is_del = false ; request.ExtraData.Is_show = true ; request.ExtraData.Is_Special = false ; request.ExtraData.Sort_No = 1; ServicePostResponse<Guoany_Ga_B2BGCategory_InsertOrUpdate_Response> response = proxy.InsertOrUpdateB2B_G_Category(request); // ViewData["Group_Response"] = response.Data; bool result = response.Success; return Json( "{state:success}" ); } public string Upload(HttpPostedFileBase file) { if (file == null ) { return "empty" ; } var fileName = Path.Combine(Request.MapPath( "~/Upload" ), Path.GetFileName(file.FileName)); if (Directory.Exists(Server.MapPath( "~/Upload" )) == false ) //如果不存在就创建file文件夹 { Directory.CreateDirectory(Server.MapPath( "~/Upload" )); } file.SaveAs(fileName); return "upload/" +Path.GetFileName(file.FileName); } //----------------------------------------------------------------------------------------- //前台代码 @{ ViewBag.Title = "类别列表" ; } <script type= "text/javascript" > $(function () { var pager = $( '#dg' ).datagrid().datagrid( 'getPager' ); // get the pager of datagrid pager.pagination({ buttons: [{ iconCls: 'icon-search' , handler: function () { $( '#dg' ).datagrid( "reload" ); } }, { iconCls: 'icon-add' , handler: function () { alert( 'add' ); } }, { iconCls: 'icon-edit' , handler: function () { alert( 'edit' ); }, iconCls: 'icon-remove' , handler: function () { alert( '删除' ); } }] }); }) function searchCategory() { var stopOrenable = $( '#stopOrenable' ).combobox( 'getValue' ); var txtCategoryName = $( "#txtCategoryName" ).val(); var showOrNot = $( "#showOrNot" ). is ( ":checked" ) == true ? 1 : 0; var specilCategory = $( "#specilCategory" ). is ( ":checked" ) == true ? 1 : 0; $( '#dg' ).datagrid( 'load' , { queryParams: { stopOrenable: stopOrenable, txtCategoryName: txtCategoryName, showOrNot: showOrNot, specilCategory: specilCategory } }); //var randnum = Math.floor(Math.random() * 1000000); //alert(randnum) //var urls = '/Category_Manage/GetCategoryJsonList?randnum=' + randnum; } function addCategory() { $( '#dd' ).dialog( 'open' ); } function editCategory() { } function showCategory() { } function hidenCategory() { } function enabledCategory() { } function stopCategory() { } </script> <script type= "text/javascript" > function PreviewImage(imgFile) { var filextension = imgFile.value.substring(imgFile.value.lastIndexOf( "." ), imgFile.value.length); filextension = filextension.toLowerCase(); if ((filextension != '.jpg' ) && (filextension != '.gif' ) && (filextension != '.jpeg' ) && (filextension != '.png' ) && (filextension != '.bmp' )) { alert( "对不起,系统仅支持标准格式的照片,请您调整格式后重新上传,谢谢 !" ); imgFile.focus(); } else { var path; if (document.all) //IE { imgFile. select (); path = document.selection.createRange().text; document.getElementById( "imgPreview" ).innerHTML = "" ; document.getElementById( "imgPreview" ).style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='scale',src=\"" + path + "\")" ; //使用滤镜效果 } else //FF { path = window.URL.createObjectURL(imgFile.files[0]); var i = 0; i++; $( "#imgPreview" ).append( "<img id='img" + i + "' width='120px' height='100px' src='" + path + "'/>" ); } } } function submitContent() { var formData = new FormData(); var file = document.getElementById( "upload" ).files[0]; formData.append( "file" , file); $.ajax({ type: "POST" , url: '@Url.Action("CreateCategory", "Category_Manage")' , data: formData, dataType: 'json' , contentType: false , processData: false , success: function (response) { alert( "sucess" ); }, error: function (error) { alert( "fault" ); } }); } </script> <table id= "dg" class = "easyui-datagrid" title= "商品类别列表" style= "width:100%;height:250px" data-options= "rownumbers:true,singleSelect:true,url:'/Category_Manage/GetCategoryJsonList',method:'get',toolbar:'#tb',footer:'#ft',pagination:true" > <thead> <tr> <th data-options= "field:'ParentCategoryName',width:80" >上级类目</th> <th data-options= "field:'Category_Name',width:100" >类目名称</th> <th data-options= "field:'Is_Del',width:80,align:'right'" >启用状态</th> <th data-options= "field:'Des',width:80,align:'right'" >描述</th> <th data-options= "field:'Is_Special',width:240" >是否特殊项目</th> <th data-options= "field:'Is_show',width:60,align:'center'" >展示状态</th> <th data-options= "field:'Sort_No',width:60,align:'center'" >排序</th> </tr> </thead> </table> <div id= "tb" style= "padding:2px 5px;" > <div> <input type= "button" value= "新增" onclick= "addCategory()" /> <input type= "button" value= "编辑" onclick= "editCategory()" /> <input type= "button" value= "展示" onclick= "showCategory()" /> <input type= "button" value= "隐藏" onclick= "hidenCategory()" /> <input type= "button" value= "启用" onclick= "enabledCategory()" /> <input type= "button" value= "停用" onclick= "stopCategory()" /> </div> 类目名称 <input id= "txtCategoryName" style= "width:110px" > 是否停用 < select id= "stopOrenable" class = "easyui-combobox" panelHeight= "auto" style= "width:100px" > <option value= "1" >是</option> <option value= "0" >否</option> </ select > <input type= "checkbox" id= "specilCategory" /> 特殊类目 <input type= "checkbox" id= "showOrNot" /> 是否展示 <a href= "#" id= "search" class = "easyui-linkbutton" onclick= "searchCategory()" iconCls= "icon-search" >搜索</a> </div> <div id= "dd" class = "easyui-dialog" title= "新增/编辑" data-options= "iconCls:'icon-save',closed:true,modal: true,cache: false" style= "width:800px;height:600px;padding:10px" > @ using (Html.BeginForm( "CreateCategory" , "Category_Manage" , FormMethod.Post, new { enctype = "multipart/form-data" })) { <table style= "border:1px solid ;width:700px;height:500px;" > <tr> <td>父类目:<input type= "text" /></td> <td colspan= "2" > 图片上传:<input type= "file" name= "file" style= "width: 200px; height: 20px;" onchange= "PreviewImage(this)" id= "upload" /> <div id= "imgPreview" style= "width:120px; height:100px;margin-left: 280px;" ></div> </td> </tr> <tr><td>类目:<input type= "text" /></tr> <tr><td>是否特殊类目:<input type= "checkbox" /></td><td>是否显示:<input type= "checkbox" /></td></tr> <tr><td rowspan= "1" >类描述<textarea rows= "2" ></textarea></td></tr> <tr><td rowspan= "1" ><input onclick= "submitContent()" value= "提交" /></td></tr> </table> } </div> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本