Hi,

I'm trying to use the jQuery Form Plugin for file upload with JSON
response (basically, I need to get an uploaded file ID back from the
server after upload)

I have a problem when getting the data from the iframe back to the
javascript in the main page because the json string is wrapped in a
<pre></pre>.
I tried to find where those <pre> tag are added to the response but
found nothing in the jquery.form.js

So the plugin script fail when trying to evaluate the json string at
the following line in the cb function :
eval("data = " + data);
That's normal, because the data is not a correct json string, because
of those <pre> tags

The form I use contains only a <input type="file"> and a submit
button :

<form id="uploadForm" action="json/uploadFile" method="post"
enctype="multipart/form-data">
        <input type="file" id="fileUpload" name="fileUpload" type="submit" />
        <button id="buttonUpload" class="jqui-button" style="margin:5px
0px;">
                <img src="icons/Misc/document_upload.gif" alt="Upload" 
width="16"
height="16" align="absmiddle" />&nbsp;&nbsp;<span
id="buttonUploadText">Attach file</span>
        </button>
</form>

Here is how I setup my ajaxForm :

$(document).ready(function () {
        var options = {
                dataType : "json",
                iframe : true,
                success : function(data) {
                        alert("success");
                },
                error : function(data) {
                        alert("error");
                }
        };
        $('#uploadForm').jsonForm(options);
});

After selecting a file and push the "Attach file" button, the "error"
function is launched because the eval() function failed in the cb
function.

As a temporary workaround, I added the following line the the
jquer.form.js before the eval call :
data = data.replace("<pre>", "").replace("</pre>", "");

But I should not be using the plugin the right way... I'll appreciate
a lot your help if see something wrong or forgotten in my code.

I also had to set no content-type in the response header to avoid the
browser download manager to open the download window caused by a
"application/json" content-type.

Thanks for your support

Florent Paillard

Reply via email to