<textarea name="problemBody" id="content" style="display:none;"></textarea>
<iframe
src="<%=request.getContextPath() %>/edit/editor.htm?id=content&ReadCookie=0"
frameBorder="0"
marginHeight="0"
marginWidth="0"
scrolling="No"
width="621"
height="457">
</iframe>
關鍵點:id=content 必須與 textarea id 一致,編輯器會自動同步內容到該 textarea。
function chk_imgpath() {
if ($('radio1').checked) { // 外部圖片 URL 模式
const url = $('imgpath').value;
if (url === 'https://' || url === '') { window.close(); return; }
LoadIMG(url);
} else { // 本地上傳模式
const fileInput = $('file1');
if (!fileInput.value) { alert('請選擇上傳圖片文件!'); return; }
// 文件類型驗證
const filepath = fileInput.value;
const filetype = filepath.substring(filepath.lastIndexOf('.')).toLowerCase();
const allowed = ['.gif', '.jpg', '.png', '.jpeg'];
if (!allowed.includes(filetype)) {
alert('只允許上傳 .gif/.jpg/.png/.jpeg 類型文件!');
return;
}
// AJAX 文件上傳 (需 jQuery Form Plugin 或原生 FormData)
const form = $('#form1');
$.ajaxFileUpload({
type: 'POST',
secureuri: false,
fileElementId: 'file1',
url: '/ask/fileUpload?type=img',
dataType: 'json', // 建議返回 JSON
success: function(data) {
// 後端返回圖片 URL
$('#imgpath').val(data.url);
$('#radio1').prop('checked', true);
chk_imgpath(); // 遞歸調用加載圖片
$('divProcessing').show();
},
error: function() {
alert('文件上傳失敗');
}
});
}
}
@RestController
@RequestMapping("/ask")
public class UploadController {
@PostMapping("/fileUpload")
public ResponseEntity> upload(@RequestParam("file1") MultipartFile file,
@RequestParam("type") String type) {
// 1. 文件驗證 (大小、類型、內容掃描)
// 2. 存儲 (本地/對象存儲 OSS/S3/MinIO)
// 3. 生成訪問 URL
// 4. 返回標準格式
return ResponseEntity.ok(Map.of("url", "https://cdn.example.com/img/xxx.jpg"));
}
}
僅修改允許類型 (如 .pdf/.doc/.xls/.zip) 和存儲路徑,前端邏輯完全復用。
parent.document.getElementById('content') 同步數據SinaEditor 早期產品,維護狀態不明。新項目強烈建議選用活躍維護的現代編輯器,支持 React/Vue/Angular 組件化集成、TypeScript、協同編輯、Markdown 模式等現代特性。