简体   繁体   中英

How to use 'file_move_safe' for moving user uploaded files in Django

I would like to move user uploaded files, therefore I am trying to use file_move_safe to achieve this. Can someone please suggest (preferably with an example) how I could use this module to move user uploaded files in Django. I understand the module can be obtained from django.core.files.move import file_move_safe .

I've tried the below and I get an error(TypeError: coercing to Unicode: need string or buffer, ImageFieldFile found).

file_move_safe("/pictures/image1.png", "/permanent/pictures/image1.png")

import django.core.files.move

def my_view(request):
  final_file_path = determine_final_path_based_on_request(request)
  create_missing_directories(final_file_path)
  django.core.files.move.file_move_safe(
    request.FILES['myfile.txt'].temporary_file_path(),
    final_file_path
  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM