0

I feel I got lack of understanding about Buffer and File Stream, but I can't find any specific idea from other answers which is treating base64 string as a actual file.

I used 'request' package from Npm, to send a file to other server by http, multipart protocol.

The code below is working well, read a file from actual file by 'fs' package and send it by ReadStream object by createReadStream method.

(The codes is coffeescript)

#working
res = request.postSync 'http://anotherUrl/uploadDocument',
  formData: file: fs.createReadStream('/path/' + 'myfile.doc')  

What I want to do is creating a same ReadStream object by fs module from a file Based64 encoded String.

I tested something like this, but it's not working properly.

#not working
res = request.postSync 'http://anotherUrl/uploadDocument',
  formData: file: new Buffer(base64EncodedString, 'base64')

#not working
res = request.postSync 'http://anotherUrl/uploadDocument',
  formData: file: _base64ToArrayBuffer(base64EncodedString)

#not working
res = request.postSync 'http://anotherUrl/uploadDocument',
  formData: file: convertDataURIToBinary(base64EncodedString)

##(used function)
_base64ToArrayBuffer = (base64) ->
  binary_string = require('atob')(base64)
  len = binary_string.length
  bytes = new Uint8Array(len)
  i = 0
  while i < len
    bytes[i] = binary_string.charCodeAt(i)
    i++
  bytes.buffer

convertDataURIToBinary = (dataURI) ->
  BASE64_MARKER = ';base64,'
  base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length
  base64 = dataURI.substring(base64Index)
  raw = require('atob')(base64)
  rawLength = raw.length
  array = new Uint8Array(new ArrayBuffer(rawLength))
  i = 0
  while i < rawLength
    array[i] = raw.charCodeAt(i)
    i++
  array

question conclusion

The 'base64EncodedString' is validated by decode & creating file, so i don't doubt it is the matter, so I think I could have this achievement with write file from base64 and read it again with fs module, but I believe that's not the proper way.

The point of question is, 1. How could I send the base64encoded string as a ReadStream object properly in this case 2. How could I figure out relationship clearly between buffer and dataview(uint8array ...) something like that

Thanks in advance.

appendix

From @Alex Nikulin 's comment, I've tested stream-buffers package.

streamBuffers = require('stream-buffers')
myReadableStreamBuffer = new (streamBuffers.ReadableStreamBuffer)(
  frequency: 10
  chunkSize: 2048)
myReadableStreamBuffer.put base64.decode example
myReadableStreamBuffer.stop()

And when I tried bellow it failed again, and the 'AnotherUrl' returns Error message like this, 'unexpected end of part'

res = request.postSync 'http://anotherUrl/uploadDocument',
  formData: file: myReadableStreamBuffer

I figured out the myReadableStreamBuffer object is Readable object, so it might be different with ReadStream object. Can I get it as a ReadStream from myReadableStreamBuffer?

kakadais
  • 441
  • 1
  • 4
  • 17

1 Answers1

3

Try this

  //if you need just a buffer
  var base64ToBuffer = function(base64) {

        var byteString = new Buffer(base64, 'base64').toString('binary');

        var ab = new Buffer(byteString.length);
        var ia = new Uint8Array(ab);
        for (var i = 0; i < byteString.length; i++) {
            ia[i] = byteString.charCodeAt(i);
        }

        return ab;
  }
//if you need a stream, not a buffer
var stream = require('stream');

// Initiate the source
var bufferStream = new stream.PassThrough();

// Write your buffer
bufferStream.end(base64ToBuffer(base64));
bufferStream.pipe( process.stdout );
Alex Nikulin
  • 8,194
  • 4
  • 35
  • 37
  • Thanks for fast answer, but it's not working as well. The data networking was fine, but the processing of 'Another System' couldn't accept this kind of data buffer. So I think I need the same object with the ReadStream from fs pacakge.. – kakadais Jan 31 '18 at 05:59
  • Actually that part is my lack of understanding, but 'fs.createReadStream('/path/' + 'myfile.doc')' is working, and i found that the return object was ReadStream object. So I have to ask you again because of my lack of understanding.. node.js's ReadStream is ArrayBuffer or Blob? – kakadais Jan 31 '18 at 06:05
  • @jwjin no is from client side js, not nodejs.You need this https://stackoverflow.com/questions/13230487/converting-a-buffer-into-a-readablestream-in-nodejs – Alex Nikulin Jan 31 '18 at 06:08
  • I tried your suggestion and add some Appendix codes. Could you explain a little bit more to figure it out? thanks. – kakadais Jan 31 '18 at 06:35
  • @jwjin what is your question? – Alex Nikulin Jan 31 '18 at 07:12