brutusin / json-forms

JSON Schema to HTML form generator, supporting dynamic subschemas (on the fly resolution). Extensible and customizable library with zero dependencies. Bootstrap add-ons provided
http://brutusin.org/json-forms
Apache License 2.0
606 stars 168 forks source link

Support for file type field #116

Closed hxh-robb closed 6 years ago

hxh-robb commented 6 years ago

We can use decorator to render a field as a file input, but when we use bf.getData(), the value of this field is a string of file path, not a file object.

And I've taken a look at the upload demo from Brutusin-RPC, it made an extension function named bf.getFileMap().

I think it's better to make a native support in json forms.

hxh-robb commented 6 years ago

Turn out it's not that complicated to return a file object. Just override the getValue function to return the element.files[0] instead of element.value of the file input element in the decorator, then the bf.getData() will return a object instead of the file path string.

Following is the example: https://jsfiddle.net/hxh_robb/xk663z01/5/

hxh-robb commented 6 years ago

The value of file field from bf.getData() is not the original File DOM object returned by the overridden element.getValue() but a raw object. That's because brutusin makes a properties clone recursively in bf.getData(), it replaces the original value with a new clone object.

I've created a PR to fix this issue.