如何从24SevenOffice API(FileService)下载文件?

时间:2019-06-20 13:07:05

标签: javascript node.js download chunks chunking

我已经使用24SevenOffice API在Node.js中工作了一段时间,最近一次是使用FileService。我还没有从API下载文件,所以我希望就如何完成此操作提出一些建议。该文档介绍了四种下载文件的相关方法(PrepareFileDownloadDownloadFileChunckHashFileEndFileDownload)。

您需要一个API密钥和一个24sevenoffice-account。我只是在寻找有关如何完成此操作的解释:

  • 我需要使用所有四种方法来完成工作吗?
  • 这些方法做什么?
  • writestream / readstream使用哪些其他功能/方法等可能很重要?
  • chunckNum输入是什么意思? (chunckNumDownloadFileChunck方法中的输入名称)
  • 如何处理DownloadFileChunck方法的响应。 (我想这是我必须使用其他一些方法/功能的地方)

我尝试了几种将这四种方法结合起来的方法,但是我真的不知道自己在做什么,所以我决定寻求帮助。我认为我对HashFile方法并没有尝试太多。以PrepareFileDownload开头并以EndFileDownload结尾是有意义的,但是在这两者之间我该怎么办?

这仅用于显示方法。不希望此代码起作用。显然,它需要执行一些操作来存储文件内容或其他内容。该代码不包含方法HashFile

    //24sevenoffice.js is another module that takes care of logging in with the API key, username, password and all that
    const office = require("./24sevenoffice.js");
    const fs = require("fs");
    const chuncksize = ;

    //I realize that I might be on my way to callback hell here, but I will clean this up later. This is just for showing you guys.
    office.login((err) => {
        if (err){
            console.log(err);
        }

        office.service("https://webservices.24sevenoffice.com/file/V001/FileService.asmx?wsdl", (err, FileService) => {
            if (err){
                console.log(err);
            }     
            else {
                //logging in works fine
                FileService.PrepareFileDownload({FileId: 2908751, FolderId: 1256067, CheckOut: false}, (err, bytes) => {
                    if(err){
                        console.log(err);
                    }
                    else{
                        //preparefiledownload also works fine and I'm getting the TmpId and the length of the file as response.

                        console.log(bytes);

                        //My best guess for the Chuncknum input is that it states how many chuncks I want the file to be downloaded in
                        //Then it would make sense to divide the length by some constant chunksize, I hope
                        let CNUM = Math.floor(bytes.PrepareFileDownloadResult.Length/chuncksize);
                        if(CNUM == 0){
                            CNUM = 1;
                        }

                        let TMPID = new String(bytes.PrepareFileDownloadResult.TmpId);

                        //This is how I think the EndFileDownload method should look:
                        FileService.DownloadFileChunk({TmpId: TMPID, ChunckNum: CNUM}, (err, response) => {
                            if(err){
                                console.log(err);
                            }
                            else{
                                console.dir(response);
                            }
                        });

                        //This is how I think the EndFileDownload method should look:
                        FileService.EndFileDownload({TmpId: TMPID}, (err, response) => {
                            if(err){
                                console.log(err);
                            }
                            else{
                                console.log(response);
                            }
                        });
                    }
                });
            }
        });
    });

0 个答案:

没有答案