This example for you to share the implementation based on jquery Ajax upload file key code, for your reference, the specific contents are as follows
JS Code:
Save
Function Btnadd () {
var formData = new FormData ($ ("#frm") [0]);
$.ajax ({
URL: "/admin/contentmanage/saveedit",
Type: "POST",
data:formdata,
contenttype:false,// You must false to avoid jquery's default handling of Formdata XMLHttpRequest will handle the formdata correctly
Processdata:false,// Must be false to automatically add the correct content-type
success:function (data) {
if (data = = "OK") {
alert ("Save Success");
$.idialog ("close"); Refresh parent Page
}
else {
alert ("Save failed: + data";}}}
);
asp.net MVC background Code:
First to determine if the path exists and 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;
For more highlights, please click on the "jquery Upload operation Summary" for in-depth study and research.
The above is the entire content of this article, I hope to help you learn.