Hi everyone, I am trying to upload a file using fetch streams, I need can 
calculate the upload progress.

But on the server I receive an corrupt file of 0 bytes, I really don't know 
how to use well the ReadableStreams feature, that is a really new feature 
for me.

Client (Browser)

const fileReadble = (reader) => new ReadableStream({
  start(controller) {
     return pump()
      function pump() {
        return reader.read().then(({ done, value }) => {
          if (done) {
              controller.close()
              return
          }
        
          controller.enqueue(value)
          return pump()
        })
      }
  }

const uploadVideo = async (file) => {
    const fileReader = file.stream().getReader()
    const response = await fetch('http://localhost:3005', { 
      method: 'POST',   
      body: fileReadble(fileReader),
    })
  
    if (!response.ok) {
      console.error(`unexpected response ${response.statusText}`)
      return
    }
}
})

Server (Node)

import http from 'node:http'
import { createWriteStream } from 'node:fs'

http
  .createServer((req) => {
    if (req.method !== 'POST') return
    req.pipe(createWriteStream('./video.mp4'))
  })
  .listen(3005)

 

-- 
-- 
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- 
You received this message because you are subscribed to the Google Groups 
"v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-dev/b82deeb7-5f8f-462b-831b-bc640a2a6342n%40googlegroups.com.

Reply via email to