3

I have a Django app which, submitting a package, should return values that are inside it.. Submitted the form to a view called "insert":

request.FILES['file']

returns the file objects, but it is of kind < InMemoryUploadedFile>. What i need is a way to get the absolute path of the uploaded file, so that i can feed it to a method that will return the values needed

Anyone know how i can accomplish this?

Thanks

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
PirosB3
  • 1,961
  • 1
  • 17
  • 21

2 Answers2

14

Surely the name "InMemory" is a clue that the file exists in memory only, so doesn't have a path?

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
1

Daniel Roseman is correct for the context. But if you already created/inserted the object into the database using .create(...) or obj.save(). The path of the image can be retrieved by using obj.<field_name>.name. Example:

Suppose you have a model with an image field.

my_image = models.ImageFiled(upload_to='my_upload_dir')

so,

obj = ImageModel.objects.create(image=request.FILES['image']) # let's insert it to db
image_path = obj.image.name

# will return 
'my_upload_dir/myimagename.jpg'
Avishka Dambawinna
  • 1,180
  • 1
  • 13
  • 29