javascript外掛程式uploadify簡單實現檔案上傳

來源:互聯網
上載者:User

標籤:

  最近在學習mvc,需要用到檔案上傳的功能,找了很多的jquery外掛程式,最後決定使用uploadify這個外掛程式,參照了各位大神的部落格,終於勉勉強強會用了。在此,做一下筆記,方便以後忘了查看。

  首先在官網上下載uploadify外掛程式。http://www.uploadify.com/download/

  解壓後有以下檔案:

  

  然後在把下載下來的檔案解壓到專案檔夾中(只要項目可以引用到就可以了,但還是應該規範一點,養成良好的習慣)。

  

 

  再然後,就是把uploadify.css和jquery.uploadify.min.js檔案引用到頁面中(當然還需要引用jquery檔案)。

  

  在頁面建立一個type為file的input標籤(屬性name和id可以隨便取,但是在做初始化時,就要使用到自己取得name或id值了)。

  

  初始化。uploadify有一些參數需要設定的,我只設定了自己需要的參數,更多參數我參考了部落格:http://blog.sina.com.cn/s/blog_5079086b0101fkmh.html

  

   到此前端的檔案上傳已經完成了,接下來,就是後端如何處理上傳檔案了。

   

 #region 檔案上傳+public ActionResult Upload(HttpPostedFileBase fileData)        /// <summary>        /// 檔案上傳        /// </summary>        /// <returns></returns>        [AcceptVerbs(HttpVerbs.Post)]        public ActionResult Upload(HttpPostedFileBase fileData)        {            if (fileData != null)            {                try                {                    // 檔案上傳後的儲存路徑(找到自己要上傳到的檔案夾)                    string filePath = Server.MapPath("~/Areas/uploadFile");                    if (!Directory.Exists(filePath))                    {                        Directory.CreateDirectory(filePath);                    }                    string fileName = Path.GetFileName(fileData.FileName);// 原始檔案名稱                    string fileExtension = Path.GetExtension(fileName); // 副檔名                    string saveName = Guid.NewGuid().ToString() + fileExtension; // 儲存檔案名稱                    string path = filePath + "\\" + saveName;                    fileData.SaveAs(path);                    return Json(new { Success = true, FileName = fileName, SaveName = saveName });                }                catch (Exception ex)                {                    return Json(new { Success = false, Message = ex.Message }, JsonRequestBehavior.AllowGet);                }            }            else            {                return Json(new { Success = false, Message = "請選擇要上傳的檔案!" }, JsonRequestBehavior.AllowGet);            }        }        #endregion

  到這裡檔案上傳已經完全實現了,具體響應回去的檔案上傳路徑要怎麼處理,就看需求了。

javascript外掛程式uploadify簡單實現檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.