【问题标题】:django rest framework doesn't accept blob picture file (File extension “” is not allowed)django rest框架不接受blob图片文件(文件扩展名“”是不允许的)
【发布时间】:2020-11-23 13:55:44
【问题描述】:

我正在尝试通过发出包含 png blob 图像文件的 multipart/form-data 放置请求(使用 axios 从我的 vue 前端)来更新用户配置文件。我收到一条错误消息:不允许使用文件扩展名“”。 这是用户配置文件模型上的文件字段:

    profile_picture = models.FileField(
    _("Profile Pictures"),
    upload_to="profile_picture",
    max_length=100,
    blank=True,
    null=True,
)

这些是我在 Userprofile 模型中用来保存和更新模型的信号。

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)


@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

我认为这可能是因为这些或其他特定于 Userprofile 模型的东西导致了错误,因为在另一个模型上,文件上传按预期工作,尽管我正在发布一个帖子而不是一个放置请求。 Userprofile 模型通过一对一字段连接到用户。

在我的序列化程序或视图中没有什么特别之处可能导致错误。

我不知道我能做些什么来解决这个问题。感谢您的所有建议。如果您需要任何其他信息,请随时询问。

图像是随 put 请求发送的表单数据...可能有问题:

发出请求的axios代码:

import axios from 'axios'

const apiClient = axios.create({
  baseURL: `http://127.0.0.1:8000/`,
  withCredentials: false,
  headers: {
    Accept: 'application/json',
    'Content-Type': 'multipart/form-data'
  }
})

export default {
  updateUser(pk, params) {
    return apiClient.put('/users/' + pk + '/', params)
  }
}

这是我裁剪图片的部分,它是一个 dataURI,然后我将其转换为 blob 以发送到后端服务器:


  methods: {
    crop() {
      const { coordinates, canvas } = this.$refs.cropper.getResult()
      this.coordinates = coordinates
      this.file = canvas.toDataURL()
    },
    uploadImage(event) {
      const input = event.target
      if (input.files && input.files[0]) {
        // create a new FileReader to read this image and convert to base64 format
        const reader = new FileReader()
        // Define a callback function to run, when FileReader finishes its job
        reader.onload = (e) => {
          // Read image as base64 and set to imageData
          this.file = e.target.result
        }
        // Start the reader job - read file as a data url (base64 format)
        reader.readAsDataURL(input.files[0])
      }
    },
    dataURItoBlob(dataURI) {
      // convert base64 to raw binary data held in a string
      const byteString = atob(dataURI.split(',')[1])

      // separate out the mime component
      const mimeString = dataURI
        .split(',')[0]
        .split(':')[1]
        .split(';')[0]

      // write the bytes of the string to an ArrayBuffer
      const ab = new ArrayBuffer(byteString.length)
      const ia = new Uint8Array(ab)
      for (let i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i)
      }
      return new Blob([ab], { type: mimeString })
    },
    async updateUser() {
      this.crop()
      delete this.user.password2
      const formData = new FormData()
      if (this.file)
        formData.append('profile_picture', this.dataURItoBlob(this.file))
      if (this.user.username) formData.append('username', this.user.username)
      else formData.append('username', this.$auth.user.username)
      if (this.user.email) formData.append('email', this.user.email)
      if (this.user.bio) formData.append('bio', this.user.bio)
      if (this.user.password) formData.append('password', this.user.password)
      else formData.append('password', this.$auth.user.password)

      await UserFormService.updateUser(this.$auth.user.pk, formData)

      await this.$store.dispatch('users/updateUser', this.user)
      this.$auth.setUser(this.$store.state.users.user)
      this.$router.push('/users/me')

【问题讨论】:

  • 显示你发出put请求的axios代码
  • @Musa 我添加了代码,可能那里有问题...
  • 有斑点的部分在哪里?
  • @Musa 我现在添加了它,我只是从另一个 stackoverflow 答案中复制粘贴了数据类型转换的内容,但它在网站的另一部分对我有用。

标签: django django-models file-upload django-rest-framework blob


【解决方案1】:

由于您的端点需要特定的文件扩展名,因此您必须手动设置它,因为 blob 没有文件名,并且用于文件上传的默认值为 blob。 FormData.append 中还有第三个可选参数用于设置文件名

formData.append('profile_picture', this.dataURItoBlob(this.file), 'some_filename.valid_extension')

【讨论】:

  • 非常感谢我添加了 .png 扩展名,现在它可以按预期工作了
猜你喜欢
  • 2021-09-02
  • 2012-03-09
  • 1970-01-01
  • 2018-08-04
  • 2015-08-09
  • 2016-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多