Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;' />
@heeringa0

This comment has been minimized.

Copy link

@heeringa0 heeringa0 commented Jun 20, 2019

Thank you so much for publishing this, this helped me out!

@bahcetepe

This comment has been minimized.

Copy link

@bahcetepe bahcetepe commented Jun 9, 2020

Thank you very much.

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