PDA

View Full Version : Extjs 3 python django FileUpload Safari error



asalee200
15 Jun 2011, 5:44 PM
I have a file upload form on a panel and the backend is python with Django web-service.

This code work FF, IE and Chrome but doesnt work in Safari. The upload request gets processed in the backend but can't parse the fake Json response. Safari gives me a "SyntaxError: Parse error".

Extjs code:

var cfg = {
fileUpload: true,
frame: false,
border: false,
layout: 'form',
hideLabels: true,
height: 130,
labelWidth: 0,
defaults: {
allowBlank: false,
msgTarget: 'side'
},
items: [{
xtype: 'fileuploadfield',
id: 'form-file',
itemId: 'form-file',
width: 300,
margins: {top:5, right:0, bottom:0, left:0},
emptyText: 'Attach a SMOP Document',
name: 'file-path',
buttonText: '',
buttonCfg: {
iconCls: 'upload-icon'
},
listeners:{
fileselected: function(obj, filename){
this.fireEvent('upload', this, filename);
}
}
}]
};
...
this.getComponent('form-file').on('upload', this.onUpload, this);
...

onUpload: function() {
if(this.getForm().isValid()){
var timestamp = new Date().getTime();
this.getForm().submit({
scope: this,
url: CmCreate.Config.root + '/attach/upload/',
params: {
timestamp: timestamp
},
success: function(form, action){
var response = Ext.util.JSON.decode(action.response.responseText);
this.getComponent('form-file').reset();
},
failure: function(form, action){
var response = Ext.util.JSON.decode(action.response.responseText);
Ext.Msg.show({
title: 'Invalid file?',
msg: response['error'],
icon: Ext.Msg.ERROR,
buttons: Ext.Msg.OK
});
this.getComponent('form-file').reset();
}
});
}
}

Python code: views.py

file = request.FILES['file-path']
tempData = file.read()

if tempData == '':
response = HttpResponse(simplejson.dumps({'success':False, 'message': 'failure', 'error': 'File is Empty!'}))
return response

realName = str(request.POST['timestamp'])
realName += '_'
realName += str(file)

newFile = 'tempFile/'
newFile += realName

f = open(str(newFile), 'w')
f.write(tempData)

result = {
"success":True,
"message": "Success",
"fileName": str(file),
"realName": str(newFile)
}
json = simplejson.dumps(result, cls=DateTimeAwareJSONEncoder, ensure_ascii=False, indent=4)

response = HttpResponse(json, mimetype='text/html;')

return response


I am stuck on this issue for the last 2 weeks. I have tried all the solution provide online for look alike issue.

I would really be thankful, if someone could help me with this problem.

Let me know, if you need clarification or additional info.

jkrell
16 Jun 2011, 11:38 AM
This is a strange one, seems to be an issue with your JSON payload. Was able to replicate the issue on my end but changed the JSON payload to not wrap the keys in quotes and everything seems to be working good, here's the django snippet:


json = '{success:true, message:"%s", fileName:"%s", realName:"%s"}' % ("Success", str(file), str(newFile))

Hope this is helpful!

asalee200
17 Jun 2011, 7:23 AM
Thank you jkrell. It worked for me.