I'm trying to make a site to upload files in Laravel, but I can't :( , If I use PHP without a framework, everything work, but with it (Laravel), I get ERRORS and that is a problem.
This is my code:
<form id="remote_upload" method="POST">
@csrf
<input name="url" id="url" type="url" />
<input name="upload" id="upload" type="submit" />
</form>
My AJAX code:
$(document).ready(function(){
$('#remote_upload').on('submit', function(event){
event.preventDefault();
$.ajax({
url: "",
method:"POST",
data: new FormData(this),
// IF WE HAVE ANY FUNCTION TO SET UPLOAD PROGRESS FOR REMOTE UPLOAD - PLEASE TELL ME :)
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){
myXhr.upload.addEventListener('progress', progressHandlerHome, false);
myXhr.addEventListener("load", completeHandlerHome, false);
myXhr.addEventListener("error", errorHandlerHome, false);
myXhr.addEventListener("abort", abortHandlerHome, false);
}
return myXhr;
},
dataType:'JSON',
contentType: false,
cache: false,
processData: false,
// IF SUCCESS
success:function(data)
{
$('.result').html(data.link);
}
});
});
});
And this is my Controller:
protected function remote_upload(Request $request) {
$file = file_get_contents($request->url_file);
// UPLOAD FILE
if (getValue('storage_type', 'disk') == 's3') {
$path = Storage::disk('s3')->putFile('files', $file);
$storage = 's3';
} else {
$path = Storage::putFile('files', $file);
$storage = 'disk';
}
return response()->json([
'link' => $path
]);
}
I don't know if we have a package to upload files from URL in Laravel :)
Thanks
from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2Vxl82D
via IFTTT
Aucun commentaire:
Enregistrer un commentaire