- <script language="text/javascript">
- $("#myUEditor").onpaste = _onDragOver;
- /**
- * 粘贴事件触发
- */
- function _onPaste(ev) {
- if (ev && ev.clipboardData && ev.clipboardData.items) {
- var clipboardData = ev.clipboardData;
- var items = clipboardData.items;
- for (var i = 0; i < items.length; i++) {
- var item = items[i];
- if (item.kind === "file" && item.type === "image/png") {
- var imageFile = item.getAsFile();
- //页面上显示需要上传的文件
- _showUploadFile(imageFile);
- }
- }
- }
- }
- /**
- * 页面上显示需要上传的文件
- * @private
- */
- function _showUploadFile(file) {
- var reader = new FileReader();
- //判断文件类型
- if (file.type.match(/image*/)) {
- reader.onload = function (e) {
- var formData = new FormData();
- formData.append("img", file);
- //上传文件到服务器
- return _uploadToServer(formData);
- };
- reader.readAsDataURL(file);
- } else {
- console.log("此" + file.name + "不是图片文件!");
- }
- }
- /**
- * 上传文件到服务器
- * @private
- */
- function _uploadToServer(formData) {
- var xhr = new XMLHttpRequest();
- xhr.open("POST", "/action/uploadimg", true);
- xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest', 'Content-Type', 'multipart/form-data;');
- xhr.send(formData);
- xhr.onreadystatechange = function () {
- if (xhr.readyState == 4 && xhr.status == 200) { //上传成功时,触发
- _upload_img_url = xhr.responseText; //获取服务器返回的最新图片url
- }
- };
- }
- </script>
- //该片段来自于http://www.codesnippet.cn/detail/0601201511497.html
来源: http://www.codesnippet.cn/detail/0601201511497.html