initialPreview Repeat Image on Ajax Request

Web Tips Q & ACategory: PHPinitialPreview Repeat Image on Ajax Request
rafonseca asked 4 months ago

Hello guys,
I have a problem with this code:

Start your code here
var $input = $("#import-file");

var imgs = [];
$input.fileinput({
'language': 'pt-BR',
'showCaption': false,
"showRemove":false,
"showUpload":false,
browseIcon: "<i class=\"glyphicon glyphicon-picture\"></i> ",
"browseClass":"btn btn-primary btn-block",
allowedFileExtensions: ["jpg", "png", "gif"],
"purifyHtml":true,
uploadUrl: "/application/upload-file", // server upload action
uploadAsync: true,
overwriteInitial: false,
initialPreviewAsData: true,
maxFileSize: 3096,
uploadExtraData: function(previewId, index) {
return { "idImgs": JSON.stringify(imgs)};
},
maxFileCount: 5
}).on('filepreupload', function(event, data, previewId, index) {
//console.log(data);
}).on('fileuploaded', function(event, data, id, index) {
imgs[ imgs.length ] = data.response.idImg;
//console.log(data.response);
}).on("filebatchselected", function(event, files) {
// trigger upload method immediately after files are selected
$input.fileinput("upload");
}).on('filesuccessremove', function(event, id) {
console.log('Key = ' + id);
});

and php

$dm = $this->getEvent()->getApplication()->getServiceManager()->get('doctrine.documentmanager.odm_default');
$request = $this->getRequest();

if ( $request->isPost() ) {
if ( !is_dir('/var/www/apps/formSejaParceiro/public/img/uploads')) {
mkdir('/var/www/apps/formSejaParceiro/public/img/uploads', 0777, true);
}
$p1 = [];
$p2 = [];
$imgsPost = json_decode($request->getPost('idImgs'));
if ( $imgsPost ) {
foreach ( $imgsPost as $im ) {
$imgIn = $dm->getRepository( FotosDocument::class )->find( $im );
//var_dump($imgIn->getFile()); die();
$p1[] = "/img/uploads/" . $imgIn->getId(). '/' . $imgIn->getNome();
$p2[] = [
'caption' => $imgIn->getNome(),
'size' => (int) $imgIn->getFile()->getSize(),
'width' => '120px',
'url' => '/application/delete-file?id=' . $imgIn->getId(),
'key' => $imgIn->getId()
];
}
}

if ($request->getFiles('files')) {
foreach ( $request->getFiles('files') as $files) {
foreach ( $files as $file) {
$imagem = new FotosDocument();
$imagem->setNome($file['name']);
$dm->persist($imagem);
$dm->flush();

mkdir('/var/www/apps/formSejaParceiro/public/img/uploads/' . $imagem->getId(), 0777, true);

move_uploaded_file($file['tmp_name'], '/var/www/apps/formSejaParceiro/public/img/uploads/'
. $imagem->getId(). '/' . $file['name']);

$img = $dm->getRepository( FotosDocument::class )->find($imagem->getId());
$img->setFile('/var/www/apps/formSejaParceiro/public/img/uploads/'
. $imagem->getId(). '/' . $file['name']);
$dm->persist($img);
$dm->flush();

$p1[] = "/img/uploads/" . $img->getId(). '/' . $file['name'];
$p2[] = [
'caption' => $file['name'],
'size' => $file['size'],
'width' => '120px',
'url' => '/application/delete-file',
'key' => $img->getId(),
'extra' => [ 'id' => $img->getId() ]
];
}
}

//var_dump($p1); die();

return new JsonModel([
'initialPreview' => $p1,
'initialPreviewConfig' => $p2,
'append' => true,
'idImg' => $imagem->getId(),
]);

This code repeat the first image uploaded on box html... but the return show the diferent links...

for example $p1:
array(3) {
  [0]=>
  string(56) "/img/uploads/591fb5b5762dae5bdc27bff3/Offline_status.png"
  [1]=>
  string(65) "/img/uploads/591fb5b7762dae5bdc27bff6/Status_user_online_icon.png"
  [2]=>
  string(50) "/img/uploads/591fb5e6762dae5bdc27bff9/user_old.png"
}

Any idea, what happended ?

Sorry my code.. i try fix this but I still can not...

Thanks, and sorry my english is bad :)











Login or register to contribute or comment.

Login   Register