Last active
December 16, 2020 19:04
-
-
Save rjattrill/dd2d2597d4993433bcc6de7792a3a903 to your computer and use it in GitHub Desktop.
Sending YAML document from JavaScript (React)
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
import * as React from 'react' | |
function PickYamlFile() { | |
onInputChange = async (evt) => { | |
const file = evt.currentTarget.files[0] | |
const url = myHost() + '/import' | |
// Important to wrap the YAML with FormData encoding as YAML may contain % or other difficult characters. | |
const data = new FormData() | |
data.append('file', file) | |
// TODO: P2 - The error handling around this is weak | |
await fetch(url , { | |
method: 'POST', | |
// Note: Don't add any additional Content-Type or enctype header. | |
headers: { | |
'Access-Control-Allow-Origin': '*', | |
}, | |
body: data}) | |
} | |
return { | |
<input type="file" id="fileElem" accept=".yaml, .yml" style={{display: 'none'}} onChange={onInputChange} /> | |
} | |
} | |
// See also: https://gist.github.com/rjattrill/7415a9af49aeb7fa63c63c1caec8421c for a Sinatra receiver |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment