Created
September 24, 2019 19:26
-
-
Save leandroruel/d45f04b450c6c9d9dc6abfd7ccf761c1 to your computer and use it in GitHub Desktop.
A js file to use Uppy file uploads with laravel-mix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Uppy = require('@uppy/core'); | |
const XHRUpload = require('@uppy/xhr-upload'); | |
const Dashboard = require('@uppy/dashboard'); | |
const PortuguesBR = require('@uppy/locales/lib/pt_BR'); | |
/** | |
* Cria uma instância do plugin Uppy | |
* @param {String|Object} options objeto com algumas opções do plugin | |
*/ | |
window.uploadFiles = (options) => { | |
const defaults = () => { | |
return { | |
width: null, | |
height: null, | |
url: null, | |
target: null, | |
meta: {}, | |
headers: {}, | |
fieldName: 'file', | |
metaFields: null, | |
restrictions: { | |
maxFileSize: 2000, | |
}, | |
formData: false, | |
success: function (result) { | |
console.log(`upload completado com sucesso: ${result}`); | |
} | |
} | |
}; | |
options = Object.assign(defaults, options); | |
Uppy({ | |
meta: options.meta, | |
}) | |
.use(Dashboard, { | |
width: options.width, | |
height: options.height, | |
target: options.target, | |
inline: true, | |
showProgressDetails: true, | |
restrictions: options.restrictions, | |
locale: PortuguesBR | |
}) | |
.use(XHRUpload, { | |
endpoint: options.url, | |
headers: options.headers, | |
fieldName: options.fieldName, | |
metaFields: options.metaFields, | |
formData: options.formData | |
}) | |
.on('complete', options.success); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment