網頁另存新檔遇到的問題

2010年1月25日 星期一

今天剛好有寫到一個網頁另存新增的功能
遇到
document.execCommand('Saveas',true, 'abc.xml')
怎麼測試都不會出現另存網頁,於是google了一下
發現這是Microsoft為了安全性的限制

execcommand涵數匯出除了txt,htm,html,以外的格式都不接受
所以我就改成
document.execCommand('Saveas',true, 'abc.txt')
雖然可以了,因為我要匯出的格式是xml的格式,但是給.txt的話,他就會匯出一般的文字格式
tag會不見
於是在改成
document.execCommand('Saveas',true, 'abc.htm')
就可以正確的匯出了。

可參考資料
http://msdn.microsoft.com/zh-tw/library/system.windows.forms.htmldocument.execcommand(VS.80).aspx

下面是範例code
1.開新視窗另存新檔


function outExcel(atblData){
var w = window.open("about:blank", "Excel", "widht=0, height=0");
w.document.write(atblData.outerHTML);
if(w.document.execCommand('Saveas',true, 'abc.txt'))
alert("匯出成功");
else
alert("匯出失敗!");
window.close();
}


2.把資料丟入iframe裡在另存新檔





Read more...