星期二, 7月 19, 2011

[jQuery] File upload using AJAX form

最近使用jQuery Form Plug & jQuery File Upload上傳檔案,如果將Server Response的content-type設定application/json的話,因為元件上傳使用iframe來達到ajax form的效果,因此在Firefox和IE瀏覽器會跳出下載的Dialog。如果要回傳json格式來交換資料的話,只能將content-type設定text/plain或text/html,等接到ajax callback事件後,再將json的字串利用JSON.parser()轉換到json object即可。


後紀:使用text/plain會被多加pre element進去

Reference:Frequently Asked Questions
Internet Explorer prompting to download a file after the upload completes

The file upload plugin makes use of an Iframe Transport module for browsers like Microsoft Internet Explorer and Opera, which do not yet supportXHR file uploads.
Iframe based uploads require a Content-type of text/plain or text/html for the JSON response - they will show an undesired download dialog if the iframe response is set to application/json.
Please have a look at the Content-Type Negotiation section of the Setup instructions.

沒有留言:

張貼留言

留個話吧:)