欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于jQuery通過jQuery.form.js插件實現(xiàn)異步上傳

 更新時間:2015年12月13日 09:21:27   作者:iceKnight  
這篇文章主要介紹了基于jQuery通過jQuery.form.js插件實現(xiàn)異步上傳,代碼很詳細,感興趣的小伙伴們可以參考一下

本文主要從前臺和后臺代碼分析了jquery.form.js實現(xiàn)異步上傳的方法,分享給大家,具體代碼如下

前臺代碼:

@{
 Layout = null;
}
<!DOCTYPE html>
<html>
<head>
 <meta name="viewport" content="width=device-width" />
 <script src="~/Scripts/jquery-1.7.2.min.js"></script>
 <script src="~/Scripts/jquery.form.js"></script>
 <title>upload</title>
</head>
<body>
 <form id="fileForm" method="post" enctype="multipart/form-data" action="/upload/upload">
  文件名稱:<input name="fileName" type="text"><br />
  上傳文件:<input name="myfile" type="file" multiple="multiple"><br />
  <input id="submitBtn" type="submit" value="提交">
  <img src="#" alt="my img" id="iceImg" width="300" height="300" style="display: block;" />
 </form>

 
 <input type="text" name="height" value="170" />
 <input id="sbtn2" type="button" value="提交表單2">

 <input type="text" name="userName" value="" />
 <script type="text/javascript">
  $(function () {
   $("#fileForm").ajaxForm({
    //定義返回JSON數(shù)據(jù),還包括xml和script格式
    //clearForm Boolean值屬性,表示是否在表單提交成功后情況表單數(shù)據(jù)
    //dataType 提交成果后返回的數(shù)據(jù)格式,可選值包括xml,json或者script
    //target 服務(wù)器返回信息去更新的頁面對象,可以是jquery選擇器字符串或者jquer對象或者DOM對象。
    //type 提交類型可以是”GET“或者”POST“
    //url 表單提交的路徑
    dataType: 'json',
    beforeSend: function () {
     //表單提交前做表單驗證
     $("#myh1").show();
    },
    success: function (data) {
     //提交成功后調(diào)用
     //alert(data.message);
     $("#iceImg").attr('src', '/upload/img/' + data.fileName);
     $("#myh1").hide();
     //防止重復(fù)提交的方法
     //1.0 清空表單數(shù)據(jù)
     $('#fileForm').clearForm();
     //2.0 禁用提交按鈕

     //3.0 跳轉(zhuǎn)頁面
    }
   });


   $("#myfile").change(function () {
    $("#submitBtn").click();
   });

   $("#iceImg").click(function () {
    $("#myfile").click();
   });
  });
 </script>
 <h1 id="myh1" style="display: none;">加載中...</h1>
</body>
</html>

后臺代碼:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace IceMvc.Controllers
{
 public class UploadController : Controller
 {
  //
  // GET: /Upload/

  public ActionResult Index()
  {
   return View();
  }

  [HttpPost]
  public ActionResult Upload()
  {
   var filesList = Request.Files;
   for (int i = 0; i < filesList.Count; i++)
   {
    var file = filesList[i];
    if (file.ContentLength > 0)
    {
     if (file.ContentLength > 5242880)
     {
      return Content("<script>alert('注冊失敗!因為您選擇圖片文件不能大于5M.');window.location='/User/Register';</script>");
     }

     //得到原圖的后綴
     string extName = System.IO.Path.GetExtension(file.FileName);
     //生成新的名稱
     string newName = Guid.NewGuid() + extName;

     string imgPath = Server.MapPath("/upload/img/") + newName;

     if (file.ContentType.Contains("image/"))
     {
      using (Image img = Image.FromStream(file.InputStream))
      {
       img.Save(imgPath);
      }
      var obj = new { fileName = newName };
      return Json(obj);
     }
     else
     {
      //return Content("<script>alert('注冊失敗!因為您未選擇圖片文件.');window.location='/User/Register';</script>");
     }
    }
   }

   return Content("");
  }

  public ActionResult Afupload()
  {
   return View();
  }
 }
}

以上就是針對jquery.form.js實現(xiàn)異步上傳的方法,希望對大家的學習有所幫助。

相關(guān)文章

  • 利用jQuery輕松實現(xiàn)單選功能

    利用jQuery輕松實現(xiàn)單選功能

    這篇文章主要介紹了利用jQuery輕松實現(xiàn)單選功能的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • jQuery實現(xiàn)圖片左右滾動特效

    jQuery實現(xiàn)圖片左右滾動特效

    這篇文章主要介紹了jQuery合作伙伴左右滾動特效,功能實現(xiàn)非常簡單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2015-08-08
  • jQuery中的read和JavaScript中的onload函數(shù)的區(qū)別

    jQuery中的read和JavaScript中的onload函數(shù)的區(qū)別

    這篇文章主要介紹了jQuery中的read和JavaScript中的onload函數(shù)的區(qū)別,這兩個函數(shù)在web編程中是最常用的,一定要搞清楚它們的區(qū)別,需要的朋友可以參考下
    2014-08-08
  • JavaScript揭秘:實現(xiàn)自動化連連看游戲

    JavaScript揭秘:實現(xiàn)自動化連連看游戲

    這篇文章主要介紹了JavaScript揭秘:實現(xiàn)自動化連連看游戲的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • 解決自定義$(id)的方法與jquery選擇器$沖突的問題

    解決自定義$(id)的方法與jquery選擇器$沖突的問題

    最近網(wǎng)站改版,原來的js都是原生js,因為當前比較jquery而且他的組件也比較多,使用方便,所以我們也采用了jquery但跟原來的$(id)沖突,經(jīng)過網(wǎng)絡(luò)很多人的推薦我們選擇了這個方法
    2014-06-06
  • jQuery中scrollTop()方法用法實例

    jQuery中scrollTop()方法用法實例

    這篇文章主要介紹了jQuery中scrollTop()方法用法,實例分析了scrollTop()方法的功能、定義及返回或設(shè)置匹配元素的滾動條的垂直偏移量時的使用技巧,需要的朋友可以參考下
    2015-01-01
  • dreamweaver 8實現(xiàn)Jquery自動提示

    dreamweaver 8實現(xiàn)Jquery自動提示

    這篇文章主要介紹了dreamweaver 8實現(xiàn)Jquery自動提示的方法,需要的朋友可以參考下
    2014-12-12
  • jquery插件推薦 jquery.cookie

    jquery插件推薦 jquery.cookie

    這里給大家介紹一款兼容各大瀏覽器的jQuery處理cookies的插件jQuery.cookie,有需要的小伙伴們可以參考下
    2014-11-11
  • jquery實現(xiàn)加載等待效果示例

    jquery實現(xiàn)加載等待效果示例

    加載等待效果想必大家都有見到過吧,其實很簡單,在本文將為大家介紹下使用jquery是如何實現(xiàn)的,感興趣的朋友可以參考下
    2013-09-09
  • jQuery中animate()方法用法實例

    jQuery中animate()方法用法實例

    這篇文章主要介紹了jQuery中animate()方法用法,以實例形式分析了animate()方法的功能、定義及具體使用技巧,具有一定的參考借鑒價值,需要的朋友可以參考下
    2014-12-12

最新評論