ajax 上传图片
Posted 鞍山老菜鸟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ajax 上传图片相关的知识,希望对你有一定的参考价值。
html:
<input type="file" id="img1" name="img1" /> <input type="file" id="img2" name="img2" /> <button id="btn_submit">multipart/form-data 提交</button>
<script> $(function () { $(‘#btn_submit‘).click(function () { var formdata = new FormData(); formdata.append("submittype", "Phone_addMN"); formdata.append("glsort2", "256"); formdata.append("content", "哈哈哈哈啊"); formdata.append("channel", "街拍"); formdata.append("lng", "123"); formdata.append("lat", "121"); formdata.append("file1", $(‘#img1‘)[0].files[0]); formdata.append("file2", $(‘#img2‘)[0].files[0]); $.ajax({ url: "xxxxxxxx.ashx", type: ‘POST‘, data: formdata, dataType: ‘JSON‘, processData: false, contentType: false, success: function (result) { if (result.state) { var n = ""; } else { } } }); }); }); </script>
C#:
var model_submit = new { glsort2 = Common.ObjectToInt(_context.Request["glsort2"]), content = Common.ObjectToString(_context.Request["content"]), channel = Common.ObjectToString(_context.Request["channel"]), Data = new List<Model.API.MicroNews.Phone_ModelSubmit.Item>(), lng = Common.ObjectToString(_context.Request["lng"]), lat = Common.ObjectToString(_context.Request["lat"]) }; for (int i = 0; i < _context.Request.Files.Count; i++) { var file = _context.Request.Files[i]; var extension = Path.GetExtension(file.FileName).ToLower(); .... }
以上是关于ajax 上传图片的主要内容,如果未能解决你的问题,请参考以下文章