以python django的名称上载的文件
我使用的代码是:以python django的名称上载的文件,python,django,file-upload,Python,Django,File Upload,我使用的代码是: def saveUploadedInventory(self, inventory_file,user_id): with open('uploaded_inventory_sheet.csv','wb+') as destination: for chunk in inventory_file.chunks(): destination.write(chunk) reader = csv.read
def saveUploadedInventory(self, inventory_file,user_id):
with open('uploaded_inventory_sheet.csv','wb+') as destination:
for chunk in inventory_file.chunks():
destination.write(chunk)
reader = csv.reader(open('uploaded_inventory_sheet.csv','rb'))
已成功上载名为Upload\u inventory\u sheet.csv的文件
但我想上传同一个文件在不同的目录,其实际名称是来自客户端
我尝试以下代码:
def saveUploadedInventory(self, inventory_file,user_id):
with open(''.join(inventory_file),'wb+') as destination:
for chunk in inventory_file.chunks():
destination.write(chunk)
reader = csv.reader(open('inventory_file','rb'))
但它给出了以下错误:
异常类型:IOError
异常值:[Errno 36]文件名也是
龙:嗯
要打开文件名,而不是加入其内容:
def saveUploadedInventory(self, inventory_file,user_id):
with open(inventory_file.name,'wb+') as destination:
for chunk in inventory_file.chunks():
destination.write(chunk)
reader = csv.reader(open(inventory_file.name,'rb'))
open
采用您希望打开的文件的名称,因此您应该使用inventory\u file
的name
字段中提供的名称
发件人:
UploadedFile.name
The name of the uploaded file (e.g. my_file.txt).
“”的输出是什么?联接(库存文件)?它看起来很长,而你真正想要的只是名称。我得到错误
文件名太长了,然后文件的所有数据都被打印在上面,以及如何将该文件写入不同的目录而不是project foderTo写入不同的文件夹并打开(/path/to/inventory\u file.name,'wb+'))作为目的地
。