輕量級web富文字框——wangEditor使用手冊(6)——配置“上傳圖片”功能

來源:互聯網
上載者:User

標籤:

1. 引言

上一節講解了在wangEditor中“插入代碼”的功能,用到了彈出框。這一節的“上傳圖片”也得用彈出框。彈出框菜單如何配置,在上一節已經講過,此處將不再重複描述,只講上傳圖片的重點內容。

其實,真正的上傳圖片的功能,不是我自己做的,而是借用了一個很強大的上傳外掛程式——uploadify——好多朋友應該知道這個東西。那麼我們就來看看,如何把uploadify這個強大的工具,整合到wangEditor中來。

:https://github.com/wangfupeng1988/wangEditor

demo示範 & 全部配置說明:http://www.cnblogs.com/wangfupeng1988/p/4198428.html

交流QQ群:164999061

2. 介紹 uploadify & 做一個簡單的後台服務

uploadify的詳細應用不是本文的重點,網上有各種各樣的文檔可供參考。例如:http://www.cnblogs.com/fanyong/p/3736685.html

另外,上傳圖片肯定需要一個後台服務來接收圖片,我用asp.net做了一個簡單的後台服務,就一個.ashx文檔,把源碼粘貼給大家:

 1 <%@ WebHandler Language="C#" Class="data" %> 2  3 using System; 4 using System.Web; 5 using System.IO; 6  7 public class data : IHttpHandler { 8      9     public void ProcessRequest (HttpContext context) {10         context.Response.ContentType = "text/plain";11         context.Response.Charset = "utf-8";12 13         HttpPostedFile file = context.Request.Files["Filedata"];14         string uploadPath = context.Request.MapPath("/uploadedFiles/"); //網站中有一個 uploadedFiles 檔案夾,儲存上傳來的圖片15 16         string fileName = file.FileName;17         string imgUrl = "http://" + context.Request.Url.Authority + "/uploadedFiles/" + fileName;18 19         if (file != null)20         {21             if (Directory.Exists(uploadPath) == false)22             {23                 Directory.CreateDirectory(uploadPath);24             }25             file.SaveAs(uploadPath + fileName);26             context.Response.Write(imgUrl);27         }28         else29         {30             context.Response.Write("0"); 31         }32     }33  34     public bool IsReusable {35         get {36             return false;37         }38     }39 40 }
data.ashx 源碼3. 配置“上傳圖片”按鈕

和上一節一樣,如何配置功能表按鈕,前面文章已經講述的很清楚,這裡不再一一解釋,只說重點:

3.1 代碼結構

看到的代碼結構,大家應該非常熟悉了。注意畫紅框的‘modal‘,下文將展開解釋一下。

3.2 ‘modal‘

上一節“插入代碼”中,已經把‘modal‘的作用和規則都說明了。這裡把‘modal‘的源碼粘貼在下面,然後說明幾點。

 1 ‘modal‘: (function () { 2                         var fileId = wangEditor_getUniqeId(), 3                             $modal = $( 4                                 ‘<div>‘ + 5                                 ‘   <input type="file" id="‘ + fileId + ‘"/>‘ + 6                                 ‘</div>‘ 7                             ); 8  9                         //下文要通過jquery擷取 input-file 元素,因此這裡必須先渲染到dom樹中,否則擷取不了 input-file10                         $(‘body‘).append($modal);11 12                         //配置 uploadify13                         $(‘#‘ + fileId).uploadify({14                             height: 30,15                             width: 120,16                             swf: ‘uploadify/uploadify.swf‘,17                             uploader: ‘data.ashx‘,18                             buttonText: ‘選擇圖片‘,19                             multi: false,20                             fileTypeExts: ‘*.jpg; *.jpeg; *.gif; *.png‘,21                             onUploadSuccess: function (file, data, response) {22                                 //wangeditor_commonCommand(e, commandName, commandValue, callback);23                                 wangeditor_commonCommand(null, ‘insertImage‘, data);24                                 $(‘.uploadify-queue‘).hide();  //強制讓進度條消失25                             }26                         });27 28                         return $modal;29                     })()
‘modal‘ 源碼
  1. modal 需要的還是一個 $div;
  2. 在返回 $div 之前,要對其中的 input-file 元素做 uploadify 的配置;
  3. 在  uploadify 的配置中,上傳成功(onUploadSuccess )時,要執行commonCommand命令。注意,這裡不是javascript事件觸發的,因此沒有e,傳入null代替。
3.3 運行網頁

啟動後台服務,運行網頁,即可看到效果:

4. 總結

上傳圖片是富文字編輯器一個非常重要的功能,直到現在才更新出來,有點姍姍來遲的感覺。沒辦法,事情總得一步一個腳印的走。

本節用到的所有原始碼,可以在http://pan.baidu.com/s/1qWsN0Yg下載,然後用Visual Studio開啟網站、運行即可。

-------------------------------------------------------------------------------------------------------------

:https://github.com/wangfupeng1988/wangEditor

demo示範 & 全部配置說明:http://www.cnblogs.com/wangfupeng1988/p/4198428.html

交流QQ群:164999061

-------------------------------------------------------------------------------------------------------------

歡迎關注我的微博。

也歡迎關注我的教程:

《從設計到模式》《深入理解javascript原型和閉包系列》《微軟petshop4.0源碼解讀視頻》《json2.js源碼解讀視頻》

-------------------------------------------------------------------------------------------------------------

輕量級web富文字框——wangEditor使用手冊(6)——配置“上傳圖片”功能

聯繫我們

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