首页 > 解决方案 > 当我将图像上传到 django 中的数据库时,nonetype' 对象没有属性 'decode' 错误

问题描述

我是 ionic4/angular4 的新手。我需要将配置文件图片上传到数据库。我编写了代码,但我不知道它是否正确,当我上传它时,我收到了上述错误。他正在使用 Django 的后端,并且后端是正确的,只有其他请求正在接受。抱歉缩进不好。

.ts

getPicture() {
const options: CameraOptions = {
  quality: 70,
  destinationType: this.camera.DestinationType.FILE_URI,
  encodingType: this.camera.EncodingType.JPEG,
  mediaType: this.camera.MediaType.PICTURE,
  allowEdit: false,
  sourceType: this.camera.PictureSourceType.CAMERA,
  correctOrientation: true,
  saveToPhotoAlbum: false
};

this.camera.getPicture(options).then(
  imageData => {
    // imageData is either a base64 encoded string or a file URI
    // If it's base64 (DATA_URL):
    this.image = this.sanitizer.bypassSecurityTrustUrl(
      "data:Image/*;base64" + imageData
    );
    // this.imageURI = imageData;
    this.profileService
      .postInfluencerProfile(this.image, null, null)
      .subscribe(
        response => {
          console.log(response);
        },
        (error: MavinError) => {
          if (error instanceof NotFoundError) {
            alert("Not found");
          } else {
            console.log(error);
          }
        }
      );
  },
  err => {
    // Handle error
  }
);

}

服务.ts

 postInfluencerProfile(
    profilePic: File,
    name: string,
    emailId: string
  ): Observable<any> {
    const url = "****************";
    const apiKey = "************";
    const sessionID = localStorage.getItem("sessionID");
    const formdata = new FormData();
    formdata.append("file", profilePic);
    console.log(formdata);
    const loginContext = {
      user_agent:
        "Dalvik/2.1.0 (Linux; U; Android 6.0.1; Nexus Player Build/MMB29T)"
    };
    const postData = {
      api_key: apiKey,
      session_id: sessionID,
      profile_picture: profilePic,
      display_name: name,
      email_id: emailId
    };
    const httpOptions = {
      headers: new HttpHeaders({
        "Content-Type": "multipart/form-data;"
      })
    };
    // delete httpOptions.headers['Content-Type'];
    console.log(postData);
    return this.http.post(url, postData, httpOptions);
  }

标签: javascriptdjangoangularionic-frameworkionic4

解决方案


推荐阅读