发送多张图片但只有一张显示
Sending multiple images but only one shows
因此,在我的网站上创建了车辆后,我尝试上传一些图片时遇到了问题。我正在为我的网站使用 Laravel 并使用 Bootstrap 文件输入 (http://plugins.krajee.com/file-input) 来处理文件的上传,但我没有使用它们内置的 ajax 我正在存储以单独的形式显示图像并使用 Ajax 上传该形式。如果我检查 chrome 开发工具中的网络选项卡,我可以看到发送了多张图片,但我在我的控制器中只看到一张图片。
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');
}
图片:
所以我可以保存 reader 变量,而不是保存文件,它会给我 base64。我可以发送并解码它。
$('#fileupload').on('fileloaded', function (event, file, previewId, index, reader) {
console.log(reader.result);
imageData[] = reader;
});
因此,在我的网站上创建了车辆后,我尝试上传一些图片时遇到了问题。我正在为我的网站使用 Laravel 并使用 Bootstrap 文件输入 (http://plugins.krajee.com/file-input) 来处理文件的上传,但我没有使用它们内置的 ajax 我正在存储以单独的形式显示图像并使用 Ajax 上传该形式。如果我检查 chrome 开发工具中的网络选项卡,我可以看到发送了多张图片,但我在我的控制器中只看到一张图片。
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');
}
图片:
所以我可以保存 reader 变量,而不是保存文件,它会给我 base64。我可以发送并解码它。
$('#fileupload').on('fileloaded', function (event, file, previewId, index, reader) {
console.log(reader.result);
imageData[] = reader;
});