0

When I try to retrieve the File path it's shows me the result like this: "C:\fakepath\amine.jpeg" so the upload in the server is not working as a result of that problem.

 $('input[type=file]').change(function () {
              var filePath=$('#file-input').val();


         $.ajax({
              url : "{{path('upload_file')}}",
              type : 'POST',
              data: {
                   filePath : filePath,
                   method: 'post',
                   params: {
                       action: "uploadFile"
                   }
               },
              success : function(data, textStatus, jqXHR) {

                alert(data);


              }
          });

             }); 
Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
S.Amine
  • 13
  • 10
  • Are you saying it's literally inserting a folder called "fakepath"? Should it have a different folder name or should it just not be there? – BSMP Apr 25 '16 at 14:46
  • 2
    A file input does not disclose the full path of the file, just the file *name* - this is enough for the upload process to work. – Alex K. Apr 25 '16 at 14:46
  • 1
    That doesn't look like the right way to upload a file via ajax. – apokryfos Apr 25 '16 at 14:49
  • 1
    If I recall correctly, that's what certain browser (Google Chrome?) returns as file path when prompted instead of just throwing an error. It's an obvious security restriction and you shouldn't try to circumvent it. But this is a classical [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem): you **don't need** the file path to upload a file via AJAX. – Álvaro González Apr 25 '16 at 14:56
  • The file path is completely irrelevant to your server: all that's interesting is the file *name* - in this case, `amine.jpeg`. – Piskvor left the building Apr 25 '16 at 14:56

2 Answers2

2

You are doing this all wrong. You have to create a form object and send it via $.ajax. And I assume you have written the correct serverside code to save the image.

    var f =   new FormData();
    f.append('img',document.getElementById("file-input").files[0]);

    var url= "{{Your URL}}";
    $.ajax({
        url: url,
        type:"post",
        data: f,
        dataType:"JSON",
        processData: false,
        contentType: false,
        success: function (data, status)
        {
            console.log(data);
        },
        error: function (data)
        {

          if (data.status === 422) {

                console.log("upload failed");


            } else {
                console.log("upload success");

                }

    }); 
nilesh93
  • 419
  • 3
  • 17
  • No i want to send via ajax only the full_path of the file and the upload is done in my controller, because i'm working with curl to upload my files in the Owncloud server – S.Amine Apr 25 '16 at 15:23
-1

Your file by default upload to a temporary folder. You need to move it to an actual location to get access to it like in php:

move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)

For reference, see http://www.w3schools.com/php/php_file_upload.asp

Samar Rizvi
  • 300
  • 1
  • 9