import attachmentUploader from 'spatie-attachment-uploader';
attachmentUploader.init();
<div
class="js-attachment-uploader"
data-name="attachments"
data-url="{{ action('MyController@uploadAttachment') }}"
data-multiple
></div>
Available options:
name
url
multiple
max-files
max-filesize
parallel-uploads
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
This package is custom built for Spatie projects and is therefore not registered on npm. In order to install it via npm you have to go through out registry:
npm set registry https://npm.spatie.be
npm set ca null
Or you can require the package straight from Github:
npm install spatie-custom/spatie-attachment-uploader
import attachmentUploader from 'spatie-attachment-uploader';
attachmentUploader.init();
<div
class="js-attachment-uploader"
data-name="attachments"
data-url="{{ action('MyController@uploadAttachment') }}"
data-multiple
></div>
If you want to manually register uploader, you can use the createUploader
function:
import { createUploader } from 'spatie-attachment-uploader';
createUploader(element, options);
Options get passed down to Dropzone. Their docs have a full reference.
public function uploadAttachment()
{
try {
// Get your `$model`...
$file = request()->file('file');
if (!$model) throw new Exception('No model in session');
if (!$file) throw new Exception('No file in the request');
$media = $model->addMedia($file)->toCollection('attachments');
return response()->json(['id' => $media->id], Response::HTTP_OK);
} catch (Exception $e) {
return response(null, Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
When handling the form response, remember to remove the deleted media that was previously uploaded:
$attachments = collect($request->get('attachments'));
$model->getMedia('attachments')->reject(function (Media $media) use ($attachments) {
return $attachments->contains($media->id);
})->each(function (Media $media) {
$media->delete();
});
Please see CHANGELOG for more information what has changed recently.
$ npm run test
Please see CONTRIBUTING for details.
If you discover any security related issues, please contact Sebastian De Deyne instead of using the issue tracker.
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
The MIT License (MIT). Please see License File for more information.