Reputation: 2437
I am encoding file data using json_encode
and this is what I get when I send the encoded data in Javascript:
This is my php code (using codeigniter to upload a file):
$file_info = $this->upload->data();
echo json_encode($file_info);
and I use the data in my javascript file:
'onUploadSuccess' : function(file, data, response) {
alert('The file was saved to: ' + data);
}
How can I use file_name or other strings as they are encoded?!
for example how can I use:
'onUploadSuccess' : function(file, data, response) {
alert('The file name is: ' + file_name);
}
Upvotes: 0
Views: 417
Reputation: 1804
You have 3 variables in your response. To see what each one is when your code is executed, use console.log().
'onUploadSuccess' : function(file, data, response) {
console.log('\n"data" variable:')
console.log(data);
console.log('"file" variable:')
console.log(file);
console.log('\n"response" variable:')
console.log(response);
}
Now open up your javascript log (F12 in Chrome, Shift+F5 I think in firefox). The Json data should have been converted into an object. If it's in its json form, add JSON.parse(data)
.
Objects are the backbone of javascript. To select information in an object named data
, you use data.property
. So data.file_name
should return "83274983279843.jpg", data.type
would return the type, etc.
Edit: So after discussion in chat the issue was you didn't parse the JSON. Also I incorrectly told you to reverse the variable order.
Here is the fixed code:
'onUploadSuccess' : function(file, data, response) {
data = JSON.parse(data)
alert('The file : ' + data.file_type);
}
Upvotes: 1