NativeScript / nativescript-background-http

Background Upload plugin for the NativeScript framework
Apache License 2.0
101 stars 50 forks source link

Using PHP/Laravel to upload picture in server #213

Closed jujogi closed 5 years ago

jujogi commented 5 years ago

Hi,

I trying get the data sended by my Nativescript App to my Laravel API but when I debug the data request I do not how to get it.

    public function upload(Request $request){

        return $request; //is empty when I called task.on("responded", this.responded);

    }

I know that the data arrives because request headers are present:

    public function upload(Request $request){
        $all_headers = apache_request_headers();
        return $all_headers;
}
{
   "Accept-Encoding":"gzip, deflate",
   "Accept-Language":"en-us",
   "Content-Length":"43",
   "User-Agent":"impulsateapp\/1.0.0 CFNetwork\/975.0.3 Darwin\/18.2.0",
   "Cookie":"XSRF-TOKEN=eyJpdiI6ImRTeE8wNkZTdGMwWERmSisyRDhKMHc9PSIsInZhbHVlIjoic0ZBdVwvVmN4UVJ2SGZKUzJjdndvZTFpNDhUNkZQR1N6R1A3K29cL3NCdWp0NjJBRXA5VTJJU2tkajJHODJpNllhIiwibWFjIjoiMDg4ZDA1NmI2ZTBhNjk4MDNiNDFjMzMzMjQ3YjBlZDFiZWM5NWMyNDczZDI4MWEwOTBmYzdjOGE5MmIwMGEzNiJ9; laravel_session=eyJpdiI6ImhncDMxS0FDV2xiVUhzTFU2MStqK0E9PSIsInZhbHVlIjoiUmZMVmdIdzdhTjFieThpS0MxYjN4clRpSzFFZUpEQ2oyOHZabk90UWdaU2ZiaUgwR2JPWDh6TXV1SGJXNVRZRiIsIm1hYyI6IjM4OTFiOTMyYzk2ZGNmMmM4YzY0NDk4ZTZiNGM0NjQ3Yzg2YjMwYzc3NGYzOTA3ODE0YTY3MzJhOTBiODU2OTkifQ%3D%3D",
   "File-Name":"1555542083773.jpg",
   "Connection":"keep-alive",
   "Accept":"*\/*",
   "Content-Type":"multipart\/form-data; boundary=--------------formboundary11227933549",
   "Host":"localhost:8888"
}

What would be the correct way to obtain the data to be able to save them using Laravel or PHP?

Thanks for help!

NickIliev commented 5 years ago

@jujogi I don't think the issue is related to NativeScript or the used library. It seems strictly related to your serverside logic.

NickIliev commented 5 years ago

btw if you are talking about retrieving the response from the server then something like this should do the job

task.on('responded', (res) => {
                    let statusCode = res.responseCode;
                    let iosResponse = res.data;
                });
MohammedBashiru commented 4 years ago

Hi,

I trying get the data sended by my Nativescript App to my Laravel API but when I debug the data request I do not how to get it.

    public function upload(Request $request){

        return $request; //is empty when I called task.on("responded", this.responded);

    }

I know that the data arrives because request headers are present:

    public function upload(Request $request){
        $all_headers = apache_request_headers();
        return $all_headers;
}
{
   "Accept-Encoding":"gzip, deflate",
   "Accept-Language":"en-us",
   "Content-Length":"43",
   "User-Agent":"impulsateapp\/1.0.0 CFNetwork\/975.0.3 Darwin\/18.2.0",
   "Cookie":"XSRF-TOKEN=eyJpdiI6ImRTeE8wNkZTdGMwWERmSisyRDhKMHc9PSIsInZhbHVlIjoic0ZBdVwvVmN4UVJ2SGZKUzJjdndvZTFpNDhUNkZQR1N6R1A3K29cL3NCdWp0NjJBRXA5VTJJU2tkajJHODJpNllhIiwibWFjIjoiMDg4ZDA1NmI2ZTBhNjk4MDNiNDFjMzMzMjQ3YjBlZDFiZWM5NWMyNDczZDI4MWEwOTBmYzdjOGE5MmIwMGEzNiJ9; laravel_session=eyJpdiI6ImhncDMxS0FDV2xiVUhzTFU2MStqK0E9PSIsInZhbHVlIjoiUmZMVmdIdzdhTjFieThpS0MxYjN4clRpSzFFZUpEQ2oyOHZabk90UWdaU2ZiaUgwR2JPWDh6TXV1SGJXNVRZRiIsIm1hYyI6IjM4OTFiOTMyYzk2ZGNmMmM4YzY0NDk4ZTZiNGM0NjQ3Yzg2YjMwYzc3NGYzOTA3ODE0YTY3MzJhOTBiODU2OTkifQ%3D%3D",
   "File-Name":"1555542083773.jpg",
   "Connection":"keep-alive",
   "Accept":"*\/*",
   "Content-Type":"multipart\/form-data; boundary=--------------formboundary11227933549",
   "Host":"localhost:8888"
}

What would be the correct way to obtain the data to be able to save them using Laravel or PHP?

Thanks for help!

Were you able to solve this ?

jujogi commented 4 years ago

The trick is use name property and then call him in the controller.

      let request = this.createNewRequest();
      request.description = "uploading image " + file.path;
      request.headers["File-Name"] = this.currentFileNameBeingUploaded;

      const params = [
        {
          name: "image",
          filename: path.toString()
        }
      ];
      let task = this.session.multipartUpload(params, request);

In Laravel Controller:

        $photo = $request->file('image');

        $imageName = time() . '.' . $photo->getClientOriginalExtension();

        $image = $photo->move(public_path('images/uploads'), $imageName);

I hope it helped you! Regards,

MohammedBashiru commented 4 years ago

Thank you @jujogi I need to send multiple data along with the files. So I ended up having to encode files as strings and attach to my JSON object. then decode at server and store files. This worked for me very well.

Thanks for the tip

Justicea83 commented 4 years ago

Thank you @jujogi this worked perfectly after weeks of research

fgutteridge commented 4 years ago

For those of you reading, as I just was for hours and hours, trying to figure out just what you’re doing wrong... I recommend verifying your php.ini file: make sure that your max_upload_size and post_max_size limits are high enough to accommodate your needs... you won’t get any sort of error in your NS logs if they’re not. 🤷🏻‍♂️

(Edit: of course this has nothing to do with NativeScript... just pointing it out for those using both NS & Laravel 😀)

MohammedBashiru commented 4 years ago

Thats helpful to know

Regards.

Mohammed Bashiru Multi-Platform Software Engineer

Mobile: +233 546 891 427

Email: mohammedbashiru63@gmail.com mohammedbashiru63@gmail.com

Website: https:iammohammedb.com mohammedbashiru63@gmail.com

Code-In Ghana

               https://iammohammedb.com/register

KS-Ltd Key Beverage Distributor in Ghana

FOODS-INN[image: Logo (2)]

                Ghana Limited

SIMU-DRIVE

                Ghana Limited

On Mon, 18 Nov 2019 at 23:36, fgutteridge notifications@github.com wrote:

For those of you reading, as I just was for hours and hours, trying to figure out just what you’re doing wrong... I recommend verifying your php.ini file: make sure that your max_upload_size and post_max_size limits are high enough to accommodate your needs... you won’t get any sort of error in your NS logs if they’re not. 🤷🏻‍♂️

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/NativeScript/nativescript-background-http/issues/213?email_source=notifications&email_token=AIU2HT42VWGMDAOY7AC6UATQUMRHHA5CNFSM4HGYPCOKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEEMJY7Q#issuecomment-555261054, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIU2HT35SVCEXSNHHWBK3F3QUMRHHANCNFSM4HGYPCOA .