Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Created July 18, 2016 16:48
Show Gist options
  • Save primaryobjects/d6cdf5d31242a629b0e4cda1bfc4bff9 to your computer and use it in GitHub Desktop.
Save primaryobjects/d6cdf5d31242a629b0e4cda1bfc4bff9 to your computer and use it in GitHub Desktop.
Posting binary WAV file from javascript to R shiny server.
// See also http://stackoverflow.com/a/27228544/2596404
// Blob is your binary data.
// blob = ...;
// Encode the data and post to server.
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
base64data = reader.result;
// Trigger the change event on the form field to activate the reactive field on the server.
$('#audio').val(base64data).change();
// Submit the form by triggering the click event on the button.
$('#btnUpload').click();
}
shinyServer(function(input, output, session) {
observeEvent(input$btnUpload, {
# Decode wav file.
audio <- input$audio
audio <- gsub('data:audio/wav;base64,', '', audio)
audio <- gsub(' ', '+', audio)
audio <- base64Decode(audio, mode = 'raw')
# Save to file on server.
inFile <- list()
inFile$datapath <- paste0('temp', sample(1:100000, 1), '.wav')
inFile$file <- file(inFile$datapath, 'wb')
writeBin(audio, inFile$file)
close(inFile$file)
# ...
}
}
<input id='audio' type='text' style='display: none;' />
<input id='btnUpload' type='button' style='display: none;' />
@bahcetepe
Copy link

Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment