?ver= 無法徹底解決。Uploadify 初始化需載入 SWF (Flash) 與 JS,Chrome 緩存機制與 Flash 初始化時序衝突,導致渲染進程崩潰。特別是 bfcache(往返緩存)還原頁面時,Flash 物件狀態異常。
$(function(){
// 關鍵:延遲 10ms 讓瀏覽器緩存模組穩定後再初始化 Uploadify
setTimeout(function(){
$('#file_upload').uploadify({
'swf' : 'tools/uploadify/uploadify.swf',
'uploader' : 'upload.php',
'onUploadSuccess' : function(file, data, response) {
// 成功回調
}
});
}, 10);
});
$(function(){
// 1. 檢測瀏覽器支援
if (!window.swfobject || !swfobject.hasFlashPlayerVersion("10.0.0")) {
alert("請安裝 Flash Player 10+");
return;
}
// 2. 延遲初始化
setTimeout(initUploadify, 10);
function initUploadify() {
$('#file_upload').uploadify({
swf: 'tools/uploadify/uploadify.swf',
uploader: 'upload.php',
buttonText: '選擇檔案',
fileTypeDesc: '圖片檔案',
fileTypeExts: '*.jpg;*.jpeg;*.png;*.gif',
multi: true,
auto: true,
onUploadSuccess: function(file, data, response) {
console.log('上傳成功:', data);
},
onUploadError: function(file, errorCode, errorMsg) {
console.error('上傳失敗:', errorCode, errorMsg);
}
});
}
});
Flash 已死(2020/12/31 終止支援)。新專案請直接使用原生 <input type="file"> + FormData + fetch/xhr:
const input = document.getElementById('file_upload');
input.addEventListener('change', async (e) => {
const files = e.target.files;
const formData = new FormData();
for (const file of files) formData.append('files[]', file);
try {
const resp = await fetch('upload.php', { method: 'POST', body: formData });
const result = await resp.json();
console.log('上傳成功:', result);
} catch (err) {
console.error('上傳失敗:', err);
}
});
setTimeout(fn, 10) 是解決 Uploadify Chrome 崩潰的「銀彈」。但根本出路是棄用 Flash,拥抱 HTML5 File API。維護舊專案時,此延遲技巧能以最小成本修復崩潰;新專案請勿再引入 Uploadify。