0

I have a Javascript code where it appends a blank file input into a table row and a clear button.

   $('.imageFile').each( function() {

    var $this = $(this);
var $imagefile = $("<input type='file' class='imageFileRow'>").attr('name',$this.attr('name')+"[]")
               .attr('value',$this.val());

$image.append($imagefile);

            var $imageclear = $('<input />')
    .attr({
        type: 'button',
        name: 'imageClear',
        class: 'imageClear',
        value: 'Clear File'
    });

    $image.append($imageclear);

});

But what I want to know if it is possible is that if the user selects a file using the textbox above, it obviously stores the file link in that file input textbox, but I want it to store the file link in a hidden input I have in the form code below. Also if the user clicks on the 'Clear file' button, it clears the file not only in the file input textbox above but also in the hidden input as well.

Below is form code which contains the hidden input:

<form id="QandA" action="insertQuestion.php" method="post" >

<input type="hidden" class="imageFile" name="fileImage">

</form>

1 Answers1

1

As far as I know you can't access that information; the sandbox will prevent that from happening (and I'm glad it does; it's none of your business that I store my document in C:\porn\girlfriend\hellokitty.jpg ;-) )

Also; you do realize that even if you could access that information the form would only post the file location and not the actual file?

Edit/update Check out this simple testcase IE, Opera, Safari and Chrome return c:\fakepath\ + <actual filename> and I'm sure other browsers will do the same.

RobIII
  • 8,488
  • 2
  • 43
  • 93
  • Do you know how an actual file can be posted beczuse in my database I have set up a 'File' table where it stores in a fileId and their relevant files? – user1309180 Apr 06 '12 at 01:01
  • Sorry, that is what I meant, I want it to post the file location but as I the file input is not in a form, I wan't the file location to not only be sotred in a file input but to be able to be stored in a hidden input – user1309180 Apr 06 '12 at 01:08
  • 1
    You won't get the file location, it will, **literally** return *c:\fakepath\ + * so, for example, if I select `c:\users\RobIII\desktop\somefile.jpg` it will return `c:\fakepath\somefile.jpg`. You might want to check [this](http://stackoverflow.com/questions/4851595/how-to-resolve-the-c-fakepath) out. – RobIII Apr 06 '12 at 01:11
  • Which, by the way, is demonstrated by [the testcase](http://jsfiddle.net/YXnM4) I posted in my answer. The actual file will *only* be uploaded by the browser if the form submitted contains the file-input. You can't post another form and expect it to magically post data from the form containing the file-input. – RobIII Apr 06 '12 at 01:17