1.0.2 • Published 7 years ago

@spatie/attachment-uploader v1.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

spatie-attachment-uploader

Latest Version on NPM Software License

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.

Install

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

Usage

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

Options get passed down to Dropzone. Their docs have a full reference.

Server setup

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();
});

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ npm run test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please contact Sebastian De Deyne instead of using the issue tracker.

Credits

About Spatie

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.