Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
161
rated 0 times [  165] [ 4]  / answers: 1 / hits: 18745  / 15 Years ago, sat, may 2, 2009, 12:00:00

I trid to use an upload plugin for jQuery.
http://valums.com/ajax-upload/



When I set the returning respond type to json, firefox will popup a dialog asking how I like to handle the returning json object.



People have asked the same question at the upload script's author's page but no answer so far. Hopefully javascript guys here can figure out how we can handle this.



Thanks.



<script type= text/javascript>
/*<![CDATA[*/
$(document).ready(function(){

/* example 1 */
var button = $('#button1'), interval;
new AjaxUpload(button, {
//action: 'upload-test.php', // I disabled uploads in this example for security reasons
action: '/posts/upload_images/',
name: 'myfile',
responseType: 'json',
onSubmit : function(file, ext){
// change button text, when user selects file
button.text('Uploading');

// If you want to allow uploading only 1 file at time,
// you can disable upload button
this.disable();

// Uploding -> Uploading. -> Uploading...
interval = window.setInterval(function(){
var text = button.text();
if (text.length < 13){
button.text(text + '.');
} else {
button.text('Uploading');
}
}, 200);
},
onComplete: function(file, response){
var json = response;
alert(json);
button.text('Upload');

window.clearInterval(interval);

// enable upload button
this.enable();

// add file to the list
// $('<li></li>').appendTo('#example1 .files').text(json.response_text);
$('<li></li>').appendTo('#example1 .files').text(file);
}
});
});
/*]]>*/
</script>

More From » json

 Answers
2

I was looking for a solution for the same script and stumbled upon this page. I didn't found a solution online so here's how I fixed it:



@ upload-file.php:
replace



echo success.$cc; 


with



echo json_encode(array(
status' => 'success',
'id' => $picid,
'image' => $imgurl
));


@ front end:
replace



var bb=response.substr(0,7)
var idd=response.replace('success',' ');
var idb =idd.replace(/^s*|s*$/g,'');
if(bb===success)
{
$('<span></span>').appendTo('#files').html('<img src=images/'+file+' alt= width=120 height=120 style=margin:5px; />').addClass('success');
}
else
{
$('<span></span>').appendTo('#files').text(file).addClass('error');
}


with



var what = jQuery.parseJSON(response);
if(what.status == 'success')
{
$('<span id='+what.id+'></span>').appendTo('#files').html('<img src='+what.image+' alt= width=120 height=120 style=margin:5px; /><br><a href=javascript:void(0) onClick=deleteFile('+what.id+');>Delete</a>').addClass('success');
}
else
{
$('<span></span>').appendTo('#files').text(response).addClass('error');
}


And to actually answer this question.



jQuery.parseJSON(response);



does..


[#99610] Tuesday, April 28, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
judydestiniem

Total Points: 215
Total Questions: 109
Total Answers: 86

Location: Indonesia
Member since Wed, Jul 7, 2021
3 Years ago
;