perry-mitchell / webdav-client

WebDAV client written in Typescript for NodeJS and the browser
MIT License
671 stars 145 forks source link

writeStream not work but putFileContents('xxx', stream) work well #375

Open huangganggui opened 5 months ago

huangganggui commented 5 months ago

Hi, I want to use createWriteStream to upload a local file.

      const writeStream = webdavClient.createWriteStream(remoteFileUrl);
      const readStream = fs.createReadStream(localFile);
      const uploadPromise = new Promise((resolve, reject) => {
        readStream.on('error', (error)=>{
          console.log(error)
          reject()
        });

        readStream.on('data', function(chunk) {
          console.log('readtrunk',chunk);
        });

        writeStream.on('data', function(chunk) {
          console.log('writetrunk',chunk);
        });

        writeStream.on('error', (error)=>{
          console.log(error)
          reject()
        });
        writeStream.on('finish', resolve);
        writeStream.on('pipe', (src) => {
          console.log('Something is piping into the writer.');
        });
      });
      readStream.pipe(writeStream);

      await uploadPromise;

      const readStreamDebug = fs.createReadStream(localFile);
      await webdavClient.putFileContents(remoteFileUrl+'debug.txt', readStreamDebug);
      console.log('uploaded ' + remoteFileUrl);

the remoteFileUrl created, but file content is empty. but remoteFileUrl+'debug.txt' works well,

I dont know why createWriteStream not work( writeStream on data can print content data, but file is empty) . I have to uses putFileContents to walk around.

perry-mitchell commented 3 months ago

Write streams are tricky, but the tests are currently working for writing: https://github.com/perry-mitchell/webdav-client/blob/master/test/node/operations/createWriteStream.spec.ts

Are you able to work back from the tests if you haven't already moved on?