Django / Nodejs-上传大块文件

时间:2019-02-25 14:39:46

标签: python node.js django

我有一个节点服务器和一个django服务器。我尝试将大文件从节点发送到django服务器。

我使用fs.createReadStream()函数从节点服务器发送大文件。每个创建的块都将发送到django服务器。

我是Django的新手,所以我不知道如何处理块。我试图看一下文档,但对我来说似乎还不清楚。

我需要将整个文件存储在MEDIA_ROOT路径中。

var output = fs.createWriteStream("../tmp_upload/" + pathname.split('/')[0] + ".zip");
        archive.pipe(output);
        archive
        .directory('../tmp_upload/' + pathname.split('/')[0])
        .finalize();
        output.on('close', function(){
            const stats = fs.statSync("../tmp_upload/" + pathname.split('/')[0] + ".zip")
            const fileSizeInBytes = stats.size
            let content = fs.createReadStream("../tmp_upload/" + pathname.split('/')[0] + ".zip")
            var chunk_sum = 0
            var index = 0
            content.on('data', chunk => {
                chunk_sum += chunk.length
                const data = {
                    token: req.session.userToken,

                    patientfile: {
                        file: chunk.toString(),
                        sumOfChunks: chunk_sum,
                        fileSize: fileSizeInBytes,
                        index : index,
                        label: pathname.split('/')[0] + ".zip",
                        category: "OT",


                    }



            };
            index += 1

            Store.patientFileAdd(data) // here is where i send the chunk to django 


            });

这是我的nodejs函数的一部分

PatientFileAdd类(views.APIView):     Permission_classes =(AllowAny,)

def post(self, request, *args, **kwargs):
    try:

        filedata = request.data['patientfile']
        file = filedata['file']
        file_start = filedata['index']


        upload_folder = "/patient/patient_%s" % request.userprofile.patientaccount, filedata['label']
        with open(upload_folder, 'wb+') as destination:
            for chunk in file.chunks():
                destination.write(chunk)



        return toolsViews.ResponseHandler(
                status.HTTP_202_ACCEPTED,
                {
                    'patientfile': 'OK'
                }
            )

这是我的Django视图

谢谢

0 个答案:

没有答案