not work on mobile #1099

Open
opened 2019-07-09 05:59:10 +00:00 by Matsuo32 · 0 comments
Matsuo32 commented 2019-07-09 05:59:10 +00:00 (Migrated from github.com)

I try to upload on mobile but it's not work, it just work on browser only.

on ionic4

ngOnInit() {

		this.uploader.onBuildItemForm = (fileItem, form) => {
	
			form.append('foldername', String('mainfolder'));
			form.append('foldersub', String('subfolder'));
			return { fileItem, form };
		  };
	  
		  this.uploader.onBeforeUploadItem = (item: any) => {
		  };
	  
		  this.uploader.onAfterAddingFile = (file) => {
			file.withCredentials = false; 
			this.uploader.uploadAll();
		  }
	  
		  this.uploader.onCompleteItem = (item: any, response: any, status: any, headers: any) => {
			console.log('ImageUpload:uploaded:', item,response,status,headers);
		  };
	  
	  
		  this.uploader.onCompleteAll = () => {
			console.log('******* onCompleteAll *********')
		  }
	  
		  this.uploader.onWhenAddingFileFailed = (item: any, filter: any, options: any) => {
			console.log('***** onWhenAddingFileFailed ********')
		  }
	}

this is php

<?php 

header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type,  X-Requested-With, Access-Control-Allow-Headers, Authorization');
header('Access-Control-Max-Age: 1000');


if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  echo json_encode(array('status' => false));
  exit;
}

//$path = 'images/';

if (isset($_FILES['file'])) {

   $path = $_POST['foldername'];
   $path .= '/'.$_POST['foldersub'];
   $path .= '/';

  $originalName = $_FILES['file']['name'];
  $ext = '.'.pathinfo($originalName, PATHINFO_EXTENSION);
  $generatedName = md5($_FILES['file']['tmp_name']).$ext;
  //$filePath = $path.$generatedName;
  $filePath = $path.$originalName;

  //------------------ check folder exit ----------------//
     if (!is_dir($filename)) {
        mkdir($path, 0777, true);
     
     } else {
  
     }



  if (!is_writable($path)) {
    echo json_encode(array(
      'status' => false,
      'msg'    => 'Destination directory not writable.'
    ));
    exit;
  }

  if (move_uploaded_file($_FILES['file']['tmp_name'], $filePath)) {
    echo json_encode(array(
      'status'        => true,
      'originalName'  => $originalName,
      'generatedName' => $generatedName
    ));
  }


}
else {
  echo json_encode(
    array('status' => false, 'msg' => 'No file uploaded.')
  );
  exit;
}
I try to upload on mobile but it's not work, it just work on browser only. on ionic4 ``` ngOnInit() { this.uploader.onBuildItemForm = (fileItem, form) => { form.append('foldername', String('mainfolder')); form.append('foldersub', String('subfolder')); return { fileItem, form }; }; this.uploader.onBeforeUploadItem = (item: any) => { }; this.uploader.onAfterAddingFile = (file) => { file.withCredentials = false; this.uploader.uploadAll(); } this.uploader.onCompleteItem = (item: any, response: any, status: any, headers: any) => { console.log('ImageUpload:uploaded:', item,response,status,headers); }; this.uploader.onCompleteAll = () => { console.log('******* onCompleteAll *********') } this.uploader.onWhenAddingFileFailed = (item: any, filter: any, options: any) => { console.log('***** onWhenAddingFileFailed ********') } } ``` this is php ``` <?php header("Access-Control-Allow-Credentials: true"); header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS'); header('Access-Control-Allow-Headers: Content-Type, X-Requested-With, Access-Control-Allow-Headers, Authorization'); header('Access-Control-Max-Age: 1000'); if ($_SERVER['REQUEST_METHOD'] !== 'POST') { echo json_encode(array('status' => false)); exit; } //$path = 'images/'; if (isset($_FILES['file'])) { $path = $_POST['foldername']; $path .= '/'.$_POST['foldersub']; $path .= '/'; $originalName = $_FILES['file']['name']; $ext = '.'.pathinfo($originalName, PATHINFO_EXTENSION); $generatedName = md5($_FILES['file']['tmp_name']).$ext; //$filePath = $path.$generatedName; $filePath = $path.$originalName; //------------------ check folder exit ----------------// if (!is_dir($filename)) { mkdir($path, 0777, true); } else { } if (!is_writable($path)) { echo json_encode(array( 'status' => false, 'msg' => 'Destination directory not writable.' )); exit; } if (move_uploaded_file($_FILES['file']['tmp_name'], $filePath)) { echo json_encode(array( 'status' => true, 'originalName' => $originalName, 'generatedName' => $generatedName )); } } else { echo json_encode( array('status' => false, 'msg' => 'No file uploaded.') ); exit; } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: dc/ng2-file-upload#1099