我试图在我的网站上获得一个框,允许拖动多个文件并上传到我的服务器 . 到目前为止,似乎我的AJAX正在运行 . 我的上传功能如下:
function AjaxFileUpload(files, progressBar, container){
//Creates a formdata object for the upload, appends a CSRF token, the file itself and its respective name
var formData = new FormData;
formData.append('_token', CSRF_TOKEN);
for(var i = 0; i < files.length; i++){
formData.append(files[i].name, files[i]);
}
$.ajax({
url: 'upload',
type: 'post',
data: formData,
cache: false,
contentType: false,
processData: false,
error: function(jqXHR, textStatus, errorThrown){console.log(JSON.stringify(jqXHR)); console.log('AJAX Error: ' + textStatus + ": " + errorThrown);},
success: function(data){
console.log(data);
}
})
}
在服务器端处理我的文件的方式是在我的 UploadController@upload 中 . (是的,我在 web.php 中有一条POST路线 .
class UploadController extends Controller
{
public function upload(Request $request){
$arr = [];
foreach($request->all() as $file){
if(is_file($file)){
$string = str_random(16);
$size = filesize($file);
$ext = $file->guessExtension();
$file_name = $string . '.' . $ext;
$filepath = 'storage/uploads/' . Auth::user()->email . '/' . $file_name;
$file->storeAs(('uploads/' . Auth::user()->email), $file_name);
array_push($arr, [
'name' => $file_name,
'path' => $filepath,
'mime' => $ext,
'size' => $size
]);
}
}
return $arr;
}
}
当我将文件拖到浏览器上的示例框中时,控制台成功返回一个包含文件名称及其假定位置的数组,如下所示:
但是,当我检查我的目录(公共/存储/上传和存储/上传)时,我看不到任何文件 . 我必须启用设置吗?我检查了我的config / filesystems.php文件,它看起来像这样:
任何帮助是极大的赞赏 . 干杯 .
编辑:html组成我的上传框 . 没有关联的“表单”,我可以在其中指定enctype
<div data-regard="" data-id="" class="upload-container">
<div class="file-drag">
Drag onto me
</div>
<progress class="uk-progress" value="0" max="100">0%</progress>
</div>