Comments (11)
for more precision the error is:
from yii2-file-upload-widget.
ok so i read that jquery file upload need a specific json respons. i implemented it.
Can you add an example with your yii2 controller code
from yii2-file-upload-widget.
Can you show us what is the json response that your controller send?
you need something similar to this https://github.com/blueimp/jQuery-File-Upload/wiki/JSON-Response
In your controller you need tho receive the file and save it:
$file = \yii\web\UploadedFile::getInstance($model, 'attribute');
$file->saveAs($imagepath.$file->name);
then create an json string with the field that you need and return it without anything else, only the json string.
I already try this plugin but it's I change to this http://demos.krajee.com/widget-details/fileinput, since I need multiple input fields per form and with yii2-file-upload-widget it's a mess
from yii2-file-upload-widget.
@gimox @invaderhd Sorry you got me on holidays. As soon as I am back will write that info.
This is an example of the controller action handling file upload (hope it makes sense to you guys, otherwise you will have to wait till i am back):
public function actionUpload($id)
{
$tour = Tour::findOne($id);
if (!$tour) {
throw new NotFoundHttpException(Yii::t('app', 'Page not found'));
}
$picture = new TourPicture(['scenario' => 'upload']);
$picture->tour_id = $id;
$picture->image = UploadedFile::getInstance($picture, 'image');
if ($picture->image !== null && $picture->validate(['image'])) {
Yii::$app->response->getHeaders()->set('Vary', 'Accept');
Yii::$app->response->format = Response::FORMAT_JSON;
$response = [];
if ($picture->save(false)) {
$response['files'][] = [
'name' => $picture->image->name,
'type' => $picture->image->type,
'size' => $picture->image->size,
'url' => $picture->getImageUrl(),
'thumbnailUrl' => $picture->getImageUrl(TourPicture::SMALL_IMAGE),
'deleteUrl' => Url::to(['delete', 'id' => $picture->id]),
'deleteType' => 'POST'
];
} else {
$response[] = ['error' => Yii::t('app', 'Unable to save picture')];
}
@unlink($picture->image->tempName);
} else {
if ($picture->hasErrors(['picture'])) {
$response[] = ['error' => HtmlHelper::errors($picture)];
} else {
throw new HttpException(500, Yii::t('app', 'Could not upload file.'));
}
}
return $response;
}
from yii2-file-upload-widget.
ok. thanks.
The problem is not your widget but the jQuery file upload that i never used before.
So it has a specific response.
i add a json response in the correct format and it work.
So it's very confortable the option that you have added for changing template in form,upload and template. I change it for a speedy custom upload widget.
Good work Tony!
from yii2-file-upload-widget.
@gimox thanks... sorry for the delay but is due to vacation
from yii2-file-upload-widget.
No problem.
from yii2-file-upload-widget.
Is it possible to get a snippet for this widget in case of editing? Lets say you've already attached some photos to a specific model and now you want to remove some and add new?
P.S. Thanks Tony! Great work, really helped me!
from yii2-file-upload-widget.
Solved with
View
$script = "jQuery.ajax({
url: jQuery('#photos-image-form').fileupload('option', 'url') + '&read=1',
dataType: 'json',
context: jQuery('#photos-image-form')[0]
}).done(function (result) {
console.log(result);
jQuery(this).fileupload('option', 'done').call(this, jQuery.Event('done'), {result: result});
});";
$this->registerJs($script);
Controller, url with read:
if($read){
$response = [];
$array = Model::find()->where(['item_id' => $id])->all();
foreach($array as $photo){
$response['files'][] = [
'name' => $photo->title,
'type' => 'jpeg',
'size' => '123',
'url' => $photo->getFile(),
'thumbnailUrl' => $photo->getThumbnail(),
'deleteUrl' => Url::to(['delete-image', 'id' => $photo->id]),
'deleteType' => 'POST'
];
}
return json_encode($response);
}
And after deleting via delete url
Return value in delete:
return $this->actionUpload($read_id, 1);
from yii2-file-upload-widget.
@konjevicante Thanks for posting the solution here
from yii2-file-upload-widget.
the view how get callback
from yii2-file-upload-widget.
Related Issues (20)
- Change language
- maxFileSize for basic upload HOT 1
- How to select buttons to use
- How to change size for thumbnail HOT 6
- How do i change the translation of the widget? HOT 1
- working with the widget use dosamigos\fileupload\FileUploadUI; in Yii2 HOT 1
- Zero-day in jQuery-File-Upload HOT 1
- How to Upload ,Delete works in yii2 HOT 2
- SyntaxError: Unexpected token < in JSON at position 0 HOT 4
- Передача изображений в в иджет HOT 1
- using FileList action HOT 2
- Redirect to view after upload HOT 1
- Open source project needed HOT 3
- single file upload
- Chunks Overwrite Themselves
- Ukrainian localization
- composer.json update for compability problem with bootstrap 4
- How to pass images in field
- Please update blueimp-file-upload depenendcy HOT 9
- bootstrap 5 support
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yii2-file-upload-widget.