Implementation of uploading file _jquery based on jquery Ajax

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.