0

If I upload a file to my website via Ajax, and I have the path of it, is there a way I could submit this path to a Django form's built-in FileField?

zallarak
  • 5,287
  • 7
  • 38
  • 54
  • Do you actually need to submit it with form or save it into a model? In second case `obj.file.save(filename, File(open(path)))` works great. – ilvar May 10 '12 at 06:52
  • @ilvar thank you for the info. I actually need to submit it with the form, but the info you provided is also useful for the future. Any thoughts on how I could do it with the form? – zallarak May 10 '12 at 14:21

1 Answers1

1

I think you can construct a UploadedFile instance from file-like object, put it into dict {'name': temporary_uploaded_file} and provide it to form's files argument. Haven't tried it myself but should be working.

ilvar
  • 5,718
  • 1
  • 20
  • 17
  • can you please clarify what you mean by providing it to a form's file argument? So let's say I retrieve the path of the uploaded file; what is meant by providing it to the form's file argument? Thanks again. – zallarak May 11 '12 at 14:17
  • `form = FormClass(data=request.POST, files=files_dict)` – ilvar May 11 '12 at 19:37
  • I tried this: {'book_pics': }, and it failed, but when I retrieve data from request.FILES (I don't want to do it this way, but for testing purposes), I get this value: ]}>, which does work. Any idea on how to create a MultiValueDict? – zallarak May 12 '12 at 08:15
  • See my full post here: http://stackoverflow.com/questions/10562294/django-filefield-not-validating-with-a-simpleuploadedfile-object – zallarak May 12 '12 at 09:12