Skip to content

Commit 78ff914

Browse files
authored
Merge pull request #53 from VeliovGroup/upload-configs
Added accept values to filter selectable files by type or extension on file select
2 parents a93263e + cdf6a20 commit 78ff914

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

README.md

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,35 @@ Generate the form with `{{> quickform}}` or `{{#autoform}}` e.g.:
142142

143143
Autoform should be wrapped in `{{#if Template.subscriptionsReady }}` which makes sure that template level subscription is ready. Without it the picture preview won't be shown. You can see update mode example [here](https://github.com/VeliovGroup/meteor-autoform-file/issues/9).
144144

145+
## Accept configuration
146+
147+
### Usage
148+
149+
You can configure the file selector, to only allow certain types of files using the `accept` property:
150+
151+
```js
152+
Schemas.Posts = new SimpleSchema({
153+
title: {
154+
type: String,
155+
max: 60
156+
},
157+
picture: {
158+
type: String,
159+
autoform: {
160+
afFieldInput: {
161+
type: 'fileUpload',
162+
collection: 'Images',
163+
accept: 'image/*' // or use explicit ext names like .png,.jpg
164+
}
165+
}
166+
}
167+
});
168+
```
169+
170+
The accept values works makes use of the native HTML `accept` attribute. Read more at the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Unique_file_type_specifiers).
171+
172+
Please read the section on **custom upload templates** and how to integrate configs like *accept* to your custom template.
173+
145174
## Multiple images
146175

147176
Multiple images — __not fully supported yet__
@@ -191,13 +220,20 @@ Your custom file preview template data context will be:
191220
});
192221
```
193222

223+
```handlebars
224+
<template name="myFileUpload">
225+
<a href="{{file.link}}">{{file.original.name}}</a>
226+
</template>
227+
```
228+
194229
## Custom upload template
195230

196231
Your custom file upload template data context will be:
197232

198233
- *file* - FS.File instance
199234
- *progress*
200235
- *status*
236+
- *config* an object containing several configs to upload behavior, such as `accept`
201237
- Other fields from [`FileUpload` instance](https://github.com/VeliovGroup/Meteor-Files/wiki/Insert-(Upload)#fileupload-methods-and-properties)
202238

203239
```js
@@ -216,11 +252,28 @@ Your custom file upload template data context will be:
216252
```
217253

218254
```handlebars
219-
<template name="myFilePreview">
220-
<a href="{{file.link}}">{{file.original.name}}</a>
255+
<template name="myFileUpload">
256+
{{#with progress}}
257+
<!-- if there is a progress present, we can use it to determine the upload progress -->
258+
<progress value="{{this.get}}" max="100"></progress>
259+
{{/with}}
260+
{{#with file}}
261+
<!-- if there is a file present, we have a file selected for upload -->
262+
<span>Uploading {{this.name}}</span>
263+
{{else}}
264+
<!-- otherwise we provide the upload -->
265+
<input data-files-collection-upload class="form-control af-file-upload-capture" type="file" accept="{{config.accept}}" />
266+
{{/if}}
221267
</template>
222268
```
223269

270+
### Note on upload configs:
271+
272+
If you pass any config, like `accept` your upload data won't be falsey anymore,
273+
so you should update your template to the example above and check for each of the given properties.
274+
This is however backwards-compatible and will not break your older templates if you don't need any of the upload config
275+
introduced in > 2.1.4 releases.
276+
224277
## Support this project:
225278

226279
- [Become a patron](https://www.patreon.com/bePatron?u=20396046) — support my open source contributions with monthly donation

lib/client/fileUpload.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
{{/if}}
1717
{{else}}
1818
{{#if uploadTemplate}}
19-
{{>Template.dynamic template=uploadTemplate data=currentUpload}}
19+
{{>Template.dynamic template=uploadTemplate data=uploadTemplateData}}
2020
{{else}}
2121
{{#with currentUpload}}
2222
Uploading <b>{{file.name}}</b>:
2323
<progress value="{{progress.get}}" max="100"></progress>
2424
&nbsp;
2525
<span class="progress">{{progress.get}}%</span>
2626
{{else}}
27-
<input data-files-collection-upload class="form-control af-file-upload-capture" type="file" />
27+
<input data-files-collection-upload class="form-control af-file-upload-capture" type="file" accept="{{accept}}" />
2828
{{/with}}
2929
{{/if}}
3030
{{/with}}

lib/client/fileUpload.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Template.afFileUpload.onCreated(function () {
5151

5252
this.uploadTemplate = this.data.atts.uploadTemplate || null;
5353
this.previewTemplate = this.data.atts.previewTemplate || null;
54+
this.accept = this.data.atts.accept || null;
5455
this.insertConfig = Object.assign({}, this.data.atts.insertConfig || {});
5556
delete this.data.atts.insertConfig;
5657
this.insertConfig = Object.assign(this.insertConfig, _.pick(this.data.atts, Object.keys(defaultInsertOpts)));
@@ -80,6 +81,20 @@ Template.afFileUpload.helpers({
8081
uploadTemplate() {
8182
return Template.instance().uploadTemplate;
8283
},
84+
uploadTemplateData() {
85+
const instance = Template.instance();
86+
const currentUpload = instance.currentUpload.get();
87+
const { accept } = instance;
88+
89+
// here we can check for upload template configs, that have been added after 2.1.4 and return either
90+
// an object with "config" merged with "currentUpload" to stay backwards compatible as popssible
91+
if (accept) {
92+
const config = { config: { accept } };
93+
return Object.assign({}, config, currentUpload);
94+
} else {
95+
return currentUpload;
96+
}
97+
},
8398
currentUpload() {
8499
return Template.instance().currentUpload.get();
85100
},
@@ -93,6 +108,9 @@ Template.afFileUpload.helpers({
93108
return null;
94109
}
95110
return template.collection.findOne({_id});
111+
},
112+
accept() {
113+
return Template.instance().accept;
96114
}
97115
});
98116

0 commit comments

Comments
 (0)