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);


              }
          });

             }); 
5
  • 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? Commented Apr 25, 2016 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. Commented Apr 25, 2016 at 14:46
  • 1
    That doesn't look like the right way to upload a file via ajax. Commented Apr 25, 2016 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: you don't need the file path to upload a file via AJAX. Commented Apr 25, 2016 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. Commented Apr 25, 2016 at 14:56

2 Answers 2

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");

                }

    }); 
Sign up to request clarification or add additional context in comments.

1 Comment

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
-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

3 Comments

i'm working under Symfony 2 and the server where i want to upload files is Owncloud
Samar Rizvi what is the equivalece of $FILES In symfony
Probably this Stack Overflow url may help you stackoverflow.com/questions/17951294/…

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.