【问题标题】:Multiple file upload in one request IONIC一次请求上传多个文件 IONIC
【发布时间】:2018-05-17 21:45:25
【问题描述】:

有没有办法在 IONIC 3 中通过多次上传发出一个请求?我的服务器端是 PHP codeigniter。

这是我的代码:

upload(){
  const fileTransfer: FileTransferObject = this.transfer.create();

  var uploadPhoto = JSON.stringify( this.photos );

 let options : FileUploadOptions = {

    fileKey: 'file',
    fileName: 'name.jpg',
    headers: {},
    params: {
      'model': uploadPhoto
    }
 };

 console.log( JSON.stringify( options ) )

 fileTransfer.upload(this.photos, encodeURI(this.authService.apiURL + '/serviceupload'), options)
  .then(data=> {
    console.log( JSON.stringify( data),"data" );
  }, (err) => {
    // error
  })
}

【问题讨论】:

  • 分享你的离子代码。
  • @mohamedvall 我编辑我的帖子
  • 你可以使用for循环
  • 感谢你们的帮助!我想我是这样写的。 uploadPhoto = []; let options : FileUploadOptions = { fileKey: 'file', fileName: 'name.jpg', headers: {}, mimeType: "image/jpeg", params: { 'model': uploadPhoto, }
  • 嘿@MelPogz,这是否支持多个文件上传和文件传输? this.photos 变量的类型是什么?

标签: php codeigniter file-upload ionic3 ionic-native


【解决方案1】:

这是我使用 Ionic 3 的方法(在我的例子中,我使用的是图像):

//declare my payload
var payload = {
      img:[]
    }

//add images to your payload, not file path to image but string data of image, file_uri comes from camera
payload.img.push(file_uri.toString())

//passing one parameter in the http.post and stringify the payload
this.http.post('https://www.myserver.com/submit.php?jobId=' + this.job.id, JSON.stringify(payload))

//PHP code to save images to file system
//get contents of request
$postdata = file_get_contents("php://input");

//we performed JSON.stringify before sending, need to decode here
$request = json_decode($postdata,true);

//loop through the array and save to file system with unique name
foreach($request['img'] as $data){

//next two lines are to get image text, removing "data:image/jpeg;base64,"
$base_to_php = explode(',', $data);
$data = base64_decode($base_to_php[1]);

//generate unique file name
$fileName = uniqid();

//save file on file system
file_put_contents('./uploads/' . $_REQUEST['job'] . '/142-' . $fileName . '.jpg',$data);
}

【讨论】:

    【解决方案2】:

    OP 的解决方案。

    我想我是这样写的。

    uploadPhoto = [];
    let options : FileUploadOptions = {
        fileKey: 'file',
        fileName: 'name.jpg',
        headers: {},
        mimeType: "image/jpeg",
        params: { 'model': uploadPhoto, }
    

    【讨论】:

      猜你喜欢
      • 2016-05-16
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      相关资源
      最近更新 更多