Upload files based on jQuery Ajax, jqueryajax
This article provides examples of key code for File Upload Based on jQuery Ajax for your reference. The specific content is as follows:
JS Code:
// SAVE function btnAdd () {var formData = new FormData ($ ("# frm") [0]); $. ajax ({url: "/Admin/ContentManage/SaveEdit", type: "POST", data: formData, contentType: false, // if it is set to false, the default processing of formdata by jQuery is avoided. XMLHttpRequest correctly processes formdata. processData: false. // The correct Content-Type success is automatically added only when it is set to false: function (data) {if (data = "OK") {alert ("saved successfully"); $. iDialog ("close"); // refresh the parent page} else {alert ("failed to save:" + data );}}});}
ASP. net mvc background code:
// First, determine whether the path exists. If the Path does not exist, create the path string Path = path. combine (System. configuration. configurationManager. appSettings ["UploadsFiles"], folder + "/" + DateTime. now. toString ("yyyyMMdd") + "/"); string physicalPath = server. mapPath (path); if (! Directory. exists (physicalPath) {Directory. createDirectory (physicalPath);} HttpPostedFileBase file = request. files [0]; string newFileName = Guid. newGuid (). toString (). replace ("-", "") + Path. getExtension (file. fileName); string savePath = Path. combine (physicalPath, newFileName); file. saveAs (savePath); fileName = file. fileName; string url = Path. combine (path, newFileName); return url;
The above is all the content of this article, hoping to help you learn.
Articles you may be interested in:
- An example of a simple jQuery plug-in ajaxfileupload. js to implement ajax File Upload
- JQuery plugin ajaxFileUpload implements asynchronous file upload
- JQuery plug-in ajaxfileupload. js uploads files asynchronously to an instance.
- JQuery asynchronous file upload plug-in ajaxFileUpload details
- PHP uses the jQuery plug-in ajaxFileUpload to Implement Asynchronous file upload.