Android-Django Image upload

后端 未结 2 1820
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-08 02:26

I am trying to upload image with android as frontend and django as backend.

The model:

    class Photo(models.Model):
        title = mo         


        
相关标签:
2条回答
  • 2021-02-08 03:05

    Should be urlString = "http://192.168.1.2/photos/upload";

    But then if it doesn't work, you'll get a different error, and we'll probably need that error to answer further.

    Also, it looks like you don't have a real boundary string set and you aren't using it correctly.

    Have a look here. Notice how he uses a unique boundary string and writes it to the output stream?

    0 讨论(0)
  • 2021-02-08 03:09

    Even after you overcome these difficulties, you'll run afoul of Django's requirement that you supply a csrfmiddlewaretoken with your POST parameters. And I don't see how you can obtain that on an Android device; by design that token is there to prevent calling Django backend code (i.e. a "view") from anything other than a Django frontend (i.e. a "template"). I.e. it's designed to thwart doing exactly what you're trying to do.

    You could disable the csrf feature on a particular view -- use the "@csrf_exempt" decorator. And then you can decide if you care enough about security, to work out a substitute of your own for what the csrf thing gives you.

    Or, instead of uploading a picture from an Android app, write a web app to upload a picture, and have your Django project serve up that web app. Your Android app could launch the browser (as an Intent) and point it at that web app. Here's some code that'll do that: https://simpleisbetterthancomplex.com/tutorial/2016/08/01/how-to-upload-files-with-django.html (It won't win any beauty contests, but it does work.)

    0 讨论(0)
提交回复
热议问题