0

hasFile does not pass validation, so it does not register

I take the screenshot and send it to the saveScreen method with the post method https://prnt.sc/Iv8GRtguBCPt output is this way, but when it comes to saving, I can't save it, I wonder exactly where I am making a mistake.

        document.getElementById('screenshotButton').addEventListener('click', function() {
            var currentURL = window.location.href;
            var parts = currentURL.split('track_warehouse/');
            var afterTrackWarehouse = parts[1];

            var csrfToken = document.head.querySelector('meta[name="csrf-token"]').content;

            html2canvas(document.body).then(function(canvas) {
                var screenshot = canvas.toDataURL('image/png');

                var formData = new FormData();
                formData.append('screenshot', screenshot);
                formData.append('afterTrackWarehouse', afterTrackWarehouse);
                formData.append('_token', csrfToken);

                var xhr = new XMLHttpRequest();
                xhr.open('POST', '/save-screenshot', true);
                xhr.onreadystatechange = function() {
                    if (xhr.readyState === 4) {
                        if (xhr.status === 200) {
                            console.log('Ekran görüntüsü başarıyla kaydedildi ve gönderildi.');
                        } else {
                            console.log('Ekran görüntüsü kaydedilirken bir hata oluştu.');
                        }
                    }
                };
                xhr.send(formData);
            });
        });




    </script>
        try {
            $product = OrderExit::where('order_uuid', $request->input('afterTrackWarehouse'))->firstOrFail();
            
            if ($request->hasFile('screenshot')) {
                $screenshot = $request->file('screenshot');
                $path = $screenshot->store('/screenShot', 'local');
                $product->screenshot_product = $path;

                $product->save();

                return response()->json(['message' => 'Resim başarıyla kaydedildi.'], 200);
            } else {
                return response()->json(['message' => 'Resim Kaydedilmedi'], 400);
            }
        } catch (Exception $exception) {
            dd($exception);
            //return response()->json(['message' => 'Resim Kaydedilmedi'],400);
        }

I tried to send the photo by wrapping it in form data but it did not work.

mehmet
  • 19
  • 4
  • 2
    _"I tried to send the photo by wrapping it in form data but it did not work."_ - you are not performing a file upload there, you are sending a Data URI. – CBroe May 15 '23 at 12:07
  • Change your backend code to this `$file = base64_decode($request['screenshot']); $fileName = str_random(10).'.'.'png'; $success = file_put_contents(public_path().'/screenShot/'.$fileName, $file);` – Kavan Prajapati May 15 '23 at 12:14
  • @CBroe I think [this question](https://stackoverflow.com/q/48195480) would also be a better dupe target here. – gre_gor May 15 '23 at 12:26

0 Answers0