This time I bring you jQuery ajax implementation File upload (with code), what are the precautions for jQuery ajax implementation of file upload, the following is a practical case, let’s take a look one time.
The main method used is the $("#formID").ajaxSubmit() method.
1. To introduce js plug-in
Attachments that need to be downloaded: jquery.form.js
2. Page code:
<script src="project/js/jquery.form.js" type="text/javascript"></script> <script src="project/js/fileload.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { createHtml($("#str")); }) </script> <tr> <td>图片</td> <td> <p id="str"> </p> <p style="display: none;" id="timgUrl"></p> <p style="color: red" id="timgok"></p> <img id="backImgUrl" src="@Model.ImageUrl" style="width:300px; height:200px;" /></td> </tr> <script src="~/project/js/jquery.form.js" type="text/javascript"></script> <script src="~/project/js/fileload.js" type="text/javascript"></script>
fileload.js:
function fileloadon() { $("#msg").html(""); $("#_fileForm").submit(function () { $("#_fileForm").ajaxSubmit({ type: "post", url: "/201410CarVideoAdmin/Home/UploadHelper", success: function (data1) { $('#timgUrl').html(data1); var reg = new RegExp('"', "g"); var imageUrl = $('#timgUrl').text().replace(reg, ""); $('#backImgUrl').attr("src", imageUrl); if ($('#timgUrl').html() != null) { $('#timgok').html("文件上传成功"); } else { $('#timgok').html("文件上传失败"); } }, error: function (msg) { alert("文件上传失败"); } }); return false; }); $("#_fileForm").submit(); }
Controller Code:
[HttpPost] public ActionResult UploadHelper() { //开始上传 string imageUrl = string.Empty; QF.WebGamePlatform.Reference.FileUploadService service = new QF.WebGamePlatform.Reference.FileUploadService(); var fileResult = service.PicUpLoad(Request.Files[0]); if (fileResult.Code == 0) { imageUrl = fileResult.Data.RawImageUrl; } return Json(imageUrl, JsonRequestBehavior.AllowGet); }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related matters on the php Chinese website article!
Recommended reading:
Detailed explanation of the steps to obtain the document object in the iframe
jQuery to obtain the iframe element
The above is the detailed content of jQuery+ajax implements file upload (with code). For more information, please follow other related articles on the PHP Chinese website!