HTML
<form role="form" id="form1"> <div class="form-group"> <label for="exampleInputFile">选择文件</label> <input type="file" name="file"> </div> <div class="col-lg-offset-3"> <input onclick="save()" value="发送" type="button" class="btn btn-success" /> </div> </form>
JS
function save() { layer.load(2); var requestid = @Model.mrequest.RequestID; $("#form1").ajaxSubmit({ url: "@Url.Action("2", "1")", type: "post", data: { "requestid": requestid }, success: function (data) { if (data.Success) { setTimeout(function () { layer.closeAll('loading'); layer.confirm(data.Message, { btn: ['返回列表', '留在此页'], //按钮 title: data.Message }, function (index) { layer.close(index); window.location.href = "@Url.Content(" /Request/Index")"; }, function (index) { location.replace(location.href); }) }) } else { layer.msg(data.Message, { time: 3000 }); } } }); }
[HttpPost] public ActionResult HSend(HttpPostedFileBase file, int requestid) { ServiceResult res = new ServiceResult(); try { string text = ConfigurationManager.AppSettings["LocalFolder"]; //上传的文件保存 if (!Directory.Exists("路径") { Directory.CreateDirectory("路径"); } //上传文件的路径以及名 string text2 = 路径 + .ToString() + ".pdf"; if (!System.IO.File.Exists(text2)) { try { file.SaveAs(text2); } catch(Exception e) { res.Success = false; res.Message = e.Message; return base.Json(res); } } catch (Exception e) { res.Success = false; res.Message =e.Message; return base.Json(res); } }
最后 如果上传大文件不成功 在web.config
<httpRuntime targetFramework="4.6.1" maxRequestLength="500000000" executionTimeout="36000"/>
<security>
<requestFiltering>
<!--500MB-->
<requestLimits maxAllowedContentLength="500000000" />
</requestFiltering>
</security>
引用js
后续在添加