laravelcontrollerjspdf

How to save PDF file from jsPDF on a server in Javascript with its real name in laravel controller?


it is my javascript code :

 var input = document.getElementById('fileUpload').files[0].name; 
         var file = doc.output('blob');
           var formData = new FormData();
           formData.append('file', file);
           $.ajaxSetup({
           headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}
                            });
        $.ajax({
                method: 'POST',
                url:'/saveEditorFiles',
                data: {formData,name:input},
                processData: false,
                contentType: false,
                success: function(data){console.log(data)},
                error: function(data){console.log(data)}
                });
            
            
            and its controller : 

       

  public function storeEditorFiles(Request $request)
            {
                $validatedData = $request->validate([
                    'file' => 'required|mimes:pdf',
                ]);
                $file = $request->name('file');
                $name = $file->getClientOriginalName();
                $uniqueName = bin2hex(random_bytes(3)) . '.' .'pdf';
                $file->move(public_path('TempFiles/'), $uniqueName);
                $save = new TempFile;
                $save->filename = $name;
                $save->user_id = Auth::user()->id;
                $save->UniqueFileName = $uniqueName;
                $save->save();
                $response = [
                    'name' => $name,
                    'file' => $file,
                ];
                return Response::json(['success' => $response]);
            }

the problem is that the file is saving in my database with the name "blob". I am trying to save it with its original name. Any help would be appreciated.


Solution

  • I found a solution append child and in second parameter pass the targeted var name:

    var realName= document.getElementById('fileUpload').files[0].name; 
     var formData = new FormData();
              formData.append('file', file);
              **formData.append('name','realName');** // realName is targeted file name
     $.ajaxSetup({
               headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}
                                });
            $.ajax({
                    method: 'POST',
                    url:'/saveEditorFiles',
                    data: {formData,name:input},
                    processData: false,
                    contentType: false,
                    success: function(data){console.log(data)},
                    error: function(data){console.log(data)}
                    });
    

    And in the controller :

    public function storeEditorFiles(Request $request)
    {
        $validatedData = $request->validate([
            'file' => 'required|mimes:pdf',
        ]);
    
        $file = $request->name('file');
        $file = $request->name('realName');
        $name = $file->getClientOriginalName();
        $uniqueName = bin2hex(random_bytes(3)) . '.' 
                    .'pdf';
                    $file->move(public_path('TempFiles/'), 
                    $uniqueName);
    
        $save = new TempFile;
        $save->filename = $name;
        $save->user_id = Auth::user()->id;
        $save->UniqueFileName = $uniqueName;
        $save->save();
    
        $response = [
                        'name' => $name,
                        'file' => $file,
                    ];
    
        return Response::json(['success' => $response]);
    }