Skip to content

Instantly share code, notes, and snippets.

@rjattrill
Last active December 16, 2020 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjattrill/dd2d2597d4993433bcc6de7792a3a903 to your computer and use it in GitHub Desktop.
Save rjattrill/dd2d2597d4993433bcc6de7792a3a903 to your computer and use it in GitHub Desktop.
Sending YAML document from JavaScript (React)
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