Created
July 18, 2016 16:48
-
-
Save primaryobjects/d6cdf5d31242a629b0e4cda1bfc4bff9 to your computer and use it in GitHub Desktop.
Posting binary WAV file from javascript to R shiny server.
This file contains 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
// 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(); | |
} |
This file contains 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
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) | |
# ... | |
} | |
} |
This file contains 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
<input id='audio' type='text' style='display: none;' /> | |
<input id='btnUpload' type='button' style='display: none;' /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much for publishing this, this helped me out!