Django 后台保存 前端上傳的圖片時報錯:django.http.multipartparser.MultiPartParserError: Invalid boundary in multipart: None


可能會遇到的錯誤:

django.http.multipartparser.MultiPartParserError: Invalid boundary in multipart: None

StackOverflow 上面有人的解答:

復制代碼
It looks like you were paying careful attention to the following note in the django-rest-framework docs:

Note: When developing client applications always remember to make sure you're setting the Content-Type header when sending data in an HTTP request.

If you don't set the content type, most clients will default to using 'application/x-www-form-urlencoded', which may not be what you wanted.
復制代碼

解決方法 就是 在提交的時候,不要添加 headers 頭部信息。

前端代碼如下:

復制代碼
uni.uploadFile({
    url:your_url,
        //  header 注釋掉
    // header: {  
    //     'Content-Type': "multipart/form-data",
    // },  
    filePath:that.imgList[0],
    name:'imgs',
    formData:{'uName': that.uName},
    success(res) {
        console.log('上傳成功!')
    }
})
復制代碼

后台代碼:

復制代碼
def submitOrders(request):
    img = request.FILES.get('imgs')
    uName = request.POST.get('uName')
    print(uName)
    print('img:', img)
    if img:
        img_path = os.path.join('static/image/', img.name)
        with open(img_path, 'wb') as fi:
            for i in img.chunks():
                fi.write(i)
復制代碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM