1

I'm using symfony and I try to import csv file to just get it content. First I added the field importFile to my Entity(ps: the field should not be stored in DB):

  /** @var  UploadedFile  $importFile*/
private $importFile;

/**
 * @return UploadedFile
 */
public function getImportFile()
{
    return $this->importFile;
}

/**
 * @param UploadedFile $importFile
 */
public function setImportFile(UploadedFile $importFile = null)
{
    $this->importFile = $importFile;
}

then I added the field to my formType:

   ->add('importFile',FileType::class, array('label' => false,'attr'=>['id'=>'upload','style'=>'']))

and in my view I aded the form field: {{ form_row(form.importFile) }}

--edit

I get the upload file on my view, but when I submit the form with ajax: -- edit2

var form = $("[id^=" + "form" + "]");
$.ajax({
        url: url,
        type: 'POST',

        data: new FormData(form),
        contentType: false,
        cache: false,
        processData:false,
        success: function (data, status)
        {
          alert('rr');
        },
        error: function (xhr, desc, err)
        {


        }
    });

I don't get the imported file on my request:

dump($request->files->all());

it returns empty .

What should I do to read the file ?

2 Answers 2

1

File inputs are not serialized by jQuery's .serialize() method. If you need to send file data to the server via AJAX, you have to resort to other methods.

A couple options are:

  1. Using FormData if your target audience's browsers support the File API.
  2. Using a jQuery plugin like jquery-form which can handle file uploads.
Sign up to request clarification or add additional context in comments.

2 Comments

do you have any example of using the jquery-form for imput file field?
using formData I have also an empty result wheen I do dump($request->files->all()); although I get the file set in the data of the ajax call(see --edit2)
0

Symfony 4 has a out of the box solution for this. Try using FormData to upload files.

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.