2017-04-09 69 views
0

我試圖從URLField下載我的文件並將其下載到ImageField。我所得到的是錯誤將文件從URLField保存到ImageField

'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte 

Unicode error hint 

The string that could not be encoded/decoded was: �PNG 

型號:

image = models.ImageField(upload_to='posts/', blank=True) 
image_url = models.URLField(null=True, blank=True) 

def save(self, *args, **kwargs): 
    if self.image_url: 
     result = urllib.request.urlretrieve(self.image_url) 
     self.image_url = '' 
     self.image.save(
      os.path.basename(self.image_url), 
      File(open(result[0])) 
     ) 
    super(Post, self).save() 

回答

0

試試這個

File(open(result[0], 'rb'))