i have a node server and a django server. I try to send large file from the node to the django server.
I send the large file from node server by using fs.createReadStream() function. Each chunk created is send to the django server.
I'am new in Django, so i don't know how to handle chunks. I tried to look the doc, but nothing seems clear for me.
I need to store entire file in MEDIA_ROOT path.
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
});
here is a part of my nodejs function
class 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'
}
)
Here my Django view
Thanks