How to save any file in the path (by dividing as chuncks) by using Django modal.

Hey,

If you are trying to upload a file to a path by using chunks format then just follow,

Please create this function as a helper function or like the utils function only.

def save_file(file,path):
    #THIS FUCNTION WILL SAVE ANY FILE THAT IS PASSED TO IT WITH THE PATH
    res={}
    orginal_filename=file.name #SAVING FILE ORGINAL NAME INTO A VARABLE
    upload_path=path + file.name #COMBINING GIVEN PATH WITH FILE NAME
    with open(upload_path,'wb+') as destination:#OPENING THAT PATH AS wb+(Opens a file for writing and reading in binary mode.)
        for chunk in file.chunks():#DIVIDS THAT FILE INTO CHUCKS AND MAKES A FOR LOOP
            destination.write(chunk)#THIS WILL TAKES THAT FILE TO THAT PATH AS CHUCKS AND COMBINES AS A ORGINAL FILE
        # DIVING AS CHUNCKS AND SAVING AT DESTINATION 
    res['path']=upload_path
    res['name']=orginal_filename
return res 

    By using the above code we can easily upload a file to a path as chuck-wise.

Sign In or Register to comment.