简体   繁体   English

如何使用'file_move_safe'在Django中移动用户上传的文件

[英]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. 我想移动用户上传的文件,因此我试图使用file_move_safe来实现这一点。 Can someone please suggest (preferably with an example) how I could use this module to move user uploaded files in Django. 有人可以建议(最好举个例子)我如何使用此模块在Django中移动用户上传的文件。 I understand the module can be obtained from django.core.files.move import file_move_safe . 我了解可以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). 我尝试了以下操作,但出现错误(TypeError:强制转换为Unicode:需要字符串或缓冲区,找到ImageFieldFile)。

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
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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