Reputation: 115
i am building a project which upload files to modify. I want to redirect to other page when uploading finishes. Redirecting is not working even though i see the response (the page html which i want to go to). here is a sample of what happens when uploading finishes:
Here is my uploading code in JavaScript:
$('#fileupload').fileupload({
dataType: 'json',
maxChunkSize: 10000000,
maxFileSize: 1000000 * 10000,
done: function (e, data) {
console.log("success");
$('#spin').css('display','none');
},
progress:function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#prog').html(progress+"%");
$('#prog').width(''+progress+'%');
if(progress == 100){
$("#prog").html('Completed');
$('#spin').css('display','block');
}
console.log(progress);
}
});
the function that saves the uploaded file (contains the redirect response) , it exists in the "UploadsController" controller:
protected function saveFile(UploadedFile $file)
{
$fileName = $file->getClientOriginalName();
$finalPath = storage_path() . '/app/upload/';
$file->move($finalPath, $fileName);//save uploaded file
if (strcmp($file->getClientOriginalExtension(), "zip") == 0) { // if it is a zip file
return redirect('download'); // redirect
} else { // if it is not a zip file
Storage::delete('/upload/' . $fileName);
return response()->json([
'status' => 'Zfalse'
]);
}
}
My routes:
Route::get('/upload','UploadsController@getUpload');
Route::post('/upload','UploadsController@postUpload');
Route::get('/download',function (){
return view('welcome');
});
Upvotes: 0
Views: 1390
Reputation: 983
You need to redirect using Javascript in the frontend, because it's an ajax request. Your ajax request is correctly being redirected, but it doesn't matter, because the redirected content that's returned is just stored in a variable inside the callback.
In your controller:
return response()->json([
'redirect' => url('to/where/you/want/to/go')
]);
In you javascript:
done: function() {
...
if (data.redirect) {
// here you actually redirect to the new page
window.location = data.redirect;
}
...
}
Upvotes: 1