0

I have a list of form such this code and need to save data with js,my code in blade file is here:

<input type='file' id="Pre_File">
<input type="text" id="Product_Name" class="profile-input">
<textarea type="text" id="Product_Desc" class="profile-input"></textarea>
 <div id="ProSave">
   <div class="card-footer">
      <button type="submit">Save</button>
   </div>
 </div>

And jquery that I used to save Product_Name and Product_Desc is here:

<script>
  $(document).ready(function (event) {
    $('#ProSave').click(function (event) {
        var Product_Name = $('#Product_Name').val();
        var Product_Desc = $('#Product_Desc').val();
        $.post('/Product/Free/Save', {
                'Product_Name': Product_Name,
                'Product_Desc': Product_Desc,
                '_token': $('input[name=_token]').val()
     });
   });
</script>

And Controller has this code:

public function Add(Request $request){
    $product = new Product;
    $product->product_name = $request->Product_Name;
    $product->product_desc = $request->Product_Desc;
    $product->save();
}

How I Can get file input and move file folder in public and save in database?

1 Answer 1

1

You cannot send a file using traditional ajax post approach, for this you have to use formData object like:

var variable1 = $("#elemId").val();
var fileData  = $('#province_upload_image').prop('files')[0];

// Create form data object and append the values into it
var formData = new FormData();
formData.append('fileData', fileData);
formData.append('variable1', variable1);
// add as many variables you want

$.ajax({
url: $('meta[name="route"]').attr('content') + '/your_route',
method: 'post',
data: formData,
contentType : false,
processData : false,
headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success: function(response){

});

Controller:

$fileData   = $request->file('fileData');
$variable1  = $request->input('variable1');
// Use it accordingly
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.