我正在尝试在我的网站上找到一个框,该框允许将多个文件拖动并上传到我的服务器上。到目前为止,看来我的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>
答案 0 :(得分:0)
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;
}
}
只需将其替换为$ file-> storeAs(('uploads /'。Auth :: user()-> email),$ file_name);到$ file-> storeAs('storage / uploads /'。Auth :: user()-> email,'public');