因此,我遇到了一个问题,即在网站上创建车辆后,我试图上传一些图像。我在网站上使用Laravel,并在使用Bootstrap File Input(http://plugins.krajee.com/file-input)来处理文件的上载,但是不是使用内置的Ajax,而是将图像存储在单独的表单中,然后使用Ajax上载该表单。如果我在chrome dev工具中检查了“网络”标签,则可以看到发送了多个图像,但是在控制器中只能看到一个图像。
HTML:
<input id="fileupload" type="file" name="images[]" multiple data-preview-file-type="text"
accepts="image/*" multiple="true" data-browse-on-zone-click="true">
存储图像:
$('#fileupload').on('fileloaded', function (event, file, previewId, index, reader) {
console.log('added image');
imageData.append('images', file);
});
Ajax:
$.ajax({
type: "POST",
url: '{{ route('ajax.newVehicleImageUpload') }}',
data: imageData,
contentType: false,
cache: false,
processData:false,
}).done( function(data) {
console.log(data);
if (data == true) {
toastr.success('Successfully uploaded images.');
}
spinner.hide();
}).fail(function(jqXHR, textStatus) {
spinner.hide();
});
PHP:
if (isset($_FILES['images'])) {
$files = $_FILES['images'];
$count = count((array) $files['name']);
for ($i = 0; $i < $count; $i++) {
$baseFileName = strtolower(str_replace(
' ',
'_',
sprintf('%s-%s-%s-%s-%s-%s.jpg',
trim($request->year),
trim($request->make),
trim(str_replace('-', '_', $request->model)),
trim(Dealer::where('id', '=', \Auth::user()->dealer_id)->first()->cpin),
trim($request->stock_number),
trim(str_random(5)))
));
$location = public_path('images/vehicles/' . $baseFileName);
Image::make(
$files['tmp_name'][$i]
)->resize(
640,
480
)->save(
$location
);
$sequence++;
$newImage = new \App\Vimage();
$newImage->sequence = $sequence;
$newImage->inventory_id = $inventory_id->id;
$newImage->vehicle_id = $request->vehicle_id;
$newImage->dealer_id = \Auth::user()->dealer_id;
$newImage->name = $baseFileName;
$newImage->isStockPhoto = false;
$newImage->isDeleted = false;
$newImage->save();
}
return ('true');
}
答案 0 :(得分:0)
因此,除了保存文件之外,我还可以保存reader变量,该变量为base64。我可以发送并解码。
$('#fileupload').on('fileloaded', function (event, file, previewId, index, reader) {
console.log(reader.result);
imageData[] = reader;
});