PDA

View Full Version : [FIXED] [OPEN]Ext.form.BasicForm fails to correctly read the response of a file upload



Tom23
28 Feb 2011, 11:16 AM
Problem: Sending a JSON as a response to a file upload form submit is quite simply not possible.

This is driving me nuts. It has been discussed in several threads but I can't believe the suggested solution is sending HTML-encoded JSON. I mean: Are you kidding?

http://www.sencha.com/forum/showthread.php?8129-Form-response-in-generated-IFrame-gets-bogus-lt-pre-gt

Another thread suggests sending JSON wrapped in a textarea. Seriously: Are you kidding me?!
http://www.sencha.com/forum/showthread.php?17248-lt-pre-gt-json-lt-pre-gt-in-File-upload-response-%28FF-only%29&p=471355&viewfull=1#post471355

Yet, the solution is ridiculously simple:


onUploadComplete: function(frame, options){
var me = this,
// bogus response object
response = {
responseText: '',
responseXML: null
}, doc, firstChild;

try {
doc = frame.contentWindow.document || frame.contentDocument || window.frames[id].document;
if (doc) {
if (doc.body) {
/* if (/textarea/i.test((firstChild = doc.body.firstChild || {}).tagName)) { // json response wrapped in textarea
response.responseText = firstChild.value;
} else {
response.responseText = doc.body.innerHTML;
} */
response.responseText = doc.body.textContent || doc.body.innerText;
}
//in IE the document may still have a body even if returns XML.
response.responseXML = doc.XMLDocument || doc;
}
} catch (e) {
}

me.fireEvent('requestcomplete', me, response, options);

Ext.callback(options.success, options.scope, [response, options]);
Ext.callback(options.callback, options.scope, [options, true, response]);

setTimeout(function(){
Ext.removeNode(frame);
}, 100);
}

Now, sending plain JSON as text/plain works.

lagnat
8 Jun 2012, 7:12 PM
bump

My $.02: The documentation says to escape html however that doesn't work properly because onUploadComplete never un-ecapes it. What we are left with is the textarea *ahem* hack which isn't documented and is susceptible to </texarea> in the content.

At the very least we need a (documented) working solution which would minimally be an HTML un-escape but OPs solution seems the more elegant way to go, yeah?