-1

I am trying to get a document to be able to pass through some AJAX and jQuery and I just keep getting the C:\fakepath\ when attempting to pass it through. I know this is a security feature within browsers, but I haven't found a way to get past it, so that it passes the doc.

Here's my code, and jsfiddle.

<form method="post" action="contact.php" name="contactform" id="contactform" enctype="multipart/form-data">
<label for="email" accesskey="E"><span class="required">*</span> FBN Document</label>
<input name="fbndoc" type="file" id="fbndoc" size="30" value="" />

jQuery(document).ready(function () {

$('#contactform').submit(function () {

    var action = $(this).attr('action');

    var values = $.map($('[name^="attribute"]'), function (elem) {
        return {
            name: elem.name,
            value: elem.value
        };
    });



    $("#message").slideUp(750, function () {
        $('#message').hide();

        $('#submit')
            .after('<img src="assets/ajax-loader.gif" class="loader" />')
            .attr('disabled', 'disabled');

        $.post(action, {
            firstname: $('#firstname').val(),
            lastname: $('#lastname').val(),
            email: $('#email').val(),
            contactphone: $('#contactphone').val(),
            values: $('values').val(),
            fbn: $('#fbn').val(),
            fbns: values,
            fbnnumber: $('#fbnnumber').val(),
            fbnaddress: $('#fbnaddress').val(),
            fbncity: $('#fbncity').val(),
            fbnstate: $('#fbnstate').val(),
            fbnzip: $('#fbnzip').val(),
            owneraddress: $('#owneraddress').val(),
            ownercity: $('#ownercity').val(),
            ownerstate: $('#ownerstate').val(),
            ownerzip: $('#ownerzip').val(),
            businesstype: $('#businesstype').val(),
            otherField: $('#otherField').val(),
            commencedate: $('#commencedate').val(),
            fbndoc: $('#fbndoc').val(),
            comments: $('#comments').val(),
            form_type: $('#form_type').val(),

            verify: $('#verify').val()
        },

        function (data) {
            document.getElementById('message').innerHTML = data;
            $('#message').slideDown('slow');
            $('#contactform img.loader').fadeOut('slow', function () {
                $(this).remove()
            });
            $('#submit').removeAttr('disabled');
            if (data.match('success') != null) $('#contactform').slideUp('slow');

        });

    });

    return false;

});

});

jsfiddle can be found here: http://jsfiddle.net/g29wQ/

halfer
  • 19,824
  • 17
  • 99
  • 186
MrTechie
  • 1,797
  • 4
  • 20
  • 36
  • 1
    If you could get *past* a security feature, it wouldn't be very secure, would it? ;) Please provide a better explanation of what you are trying to do, it is not very clear. – Felix Kling Jan 31 '14 at 17:46
  • Why do you need to know the local file structure? If you're trying to implement an ajax uploader, you're doing it wrong. – nullability Jan 31 '14 at 17:47
  • I am trying to upload a document in a form using some jquery to validate the document in the process. And if everything is submitted, then to pass all the data (document included) to the php script – MrTechie Jan 31 '14 at 17:48
  • Thanks! Completely useless to me. I will make sure I watch your posts for a -1 – MrTechie Jan 31 '14 at 18:17
  • @MrTechie my point is you can't upload a file by simply using .val(). it's far more involved than that. You can still do all of your form validation while passing the form fields into the FormData object. It is well documented here: https://developer.mozilla.org/en-US/docs/Web/API/FormData The most upvoted answer on the question i linked to also provides a good explanation of the process. – Kevin B Feb 04 '14 at 23:22

1 Answers1

2

You are sending by ajax filename, not it content:

fbndoc: $('#fbndoc').val()

If you want to upload file with it content by ajax better use FormData, for example with jQuery:

var fd = new FormData($('#fbndoc').get(0));
fd.append("CustomField", "This is some extra data");//add all you data here like this
$.ajax({
  url: "stash.php",
  type: "POST",
  data: fd,
  processData: false,  // tell jQuery not to process the data
  contentType: false   // tell jQuery not to set contentType
});
Alex
  • 11,115
  • 12
  • 51
  • 64
  • But I would still like it to do what it presently does, which checks validation for the other fields being filled out, and not just automatically send the data – MrTechie Jan 31 '14 at 17:57
  • @MrTechie and? you can still do that using FormData. – Kevin B Jan 31 '14 at 18:08
  • If I take his example, and add it to the jquery function - it just automatically pushes it to the php script, and never does any validation stuff like I'd like it to. Here's the present setup, that just pushes to the php script and not validate: http://jsfiddle.net/5M8jM/ – MrTechie Jan 31 '14 at 18:11
  • You don't want to take his example line for line, you'll need to modify the first line of teh second snippet to instead create an empty formdata object, then you'll want to populated it. See the MDN documentation for FormData for examples. – Kevin B Feb 04 '14 at 23:28