基於jQuery Ajax實現上傳檔案,jqueryajax

來源:互聯網
上載者:User

基於jQuery Ajax實現上傳檔案,jqueryajax

本文執行個體為大家分享了基於jQuery Ajax實現上傳檔案的關鍵代碼,供大家參考,具體內容如下

JS代碼:

//儲存function btnAdd() {  var formData = new FormData($("#frm")[0]);  $.ajax({    url: "/Admin/ContentManage/SaveEdit",    type: "POST",    data: formData,    contentType: false, //必須false才會避開jQuery對 formdata 的預設處理 XMLHttpRequest會對 formdata 進行正確的處理      processData: false, //必須false才會自動加上正確的Content-Type    success: function (data) {      if (data == "OK") {        alert("儲存成功");        $.iDialog("close"); //重新整理父頁面      }      else {        alert("儲存失敗:" + data);      }    }  });}

ASP.NET MVC後台代碼:

//首先判斷路徑是否存在,不存在則建立路徑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;

以上就是本文的全部內容,希望對大家的學習有所協助。

您可能感興趣的文章:
  • 一個簡單的jQuery外掛程式ajaxfileupload.js實現ajax上傳檔案例子
  • jQuery外掛程式ajaxFileUpload實現非同步上傳檔案效果
  • JQuery外掛程式ajaxfileupload.js非同步上傳檔案執行個體
  • jQuery非同步上傳檔案外掛程式ajaxFileUpload詳細介紹
  • PHP結合jQuery外掛程式ajaxFileUpload實現非同步上傳檔案執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.