简体   繁体   中英

What is the best way to send image file to server: as base64 or image file?

I have an app with REST on AngularJS/PHP(Yii) ; I'm not good in backend. I'm interesting what is the best way to upload a picture to server. I'm using Angular Material directive and get image in base64 format.

  • Should I post image as base64 ?
  • Or it's better to convert it and what format?
  • Or maybe (It's not my opinion) to upload it from html form directly to backend using Yii ?

I think you trying to upload an image file from restangular,

Try the following code:

function upload($scope.image) {
var formData = new FormData();
formData.append($scope.image);
Restangular.one('/* your url*/').withHttpConfig({transformRequest:angular.identity})
                .customPOST(formData, '', undefined, {'Content-Type': undefined})  }

by setting the content type undefined browser will automatically detect file type

that will handle your UI side. I have less idea about yii framework, but in following you can get the image file by using following type:

java : @ResponseBy : multipart/form-data ,

php : $_file

I think its better to upload file using form-data.

If you plan to display those uploaded images on a website or mobile application, you can use ImageKit.io . Easy API to upload and you get an URL for displaying that image. It will take care of optimization and also provides URL-based parameters for resizing, cropping etc.

如果您说的是文件的base64数据url表示形式,则可以将其转换为文件Blob,为其添加名称,然后将Blob作为数据文件上传。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM