Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html file picker with jQuery [duplicate]

Possible Duplicate:
Reading client side text file using Javascript

I want to open a txt file at client, parse it with javascript and post parsed data to a server page with ajax. I have scripts for parsing and posting. All i need now is to simply pick file from client computer.

What I need is something like this:

<div id="content">
     <button id="selectFile" onclick="return selectFileClick();" />
</div>

When user clicks button, a file dialog box appears and returns selected file. With this file name, I will make other operations like parsing etc.

function selectFileClick()
{
    var fileName = filedialog();
    // parsing file...
    return false;
}

Edit: I dont want to upload file and I have a custom design which doesnt look like;

<input type="file" id="file">

I need something like this: jquery file dialog plugin

Edit (2): I solved issue by this way;

$(function () {
    $("#button1").click(function (event) {
        event.preventDefault();
        $('#file').trigger('click');
    });

    document.getElementById('file').addEventListener('change', readFile, false);
});

at html;

<input id="button1" type="submit" value="add" />
<input type="file" id="file" style="display: none">

I hope this helps someone else ;)

like image 902
hakan Avatar asked Jun 12 '12 13:06

hakan


People also ask

How to clone using jQuery?

To clone an element using jQuery, use the jQuery. clone() method. The clone() method clones matched DOM Elements and select the clones. This is useful for moving copies of the elements to another location in the DOM.

How to clone HTML Element?

The cloneNode() method creates a copy of a node, and returns the clone. The cloneNode() method clones all attributes and their values. Set the deep parameter to true if you also want to clone descendants (children).

How to clone a div in HTML?

to add the div. Then we can clone the div by writing: const div = document. getElementById('foo') const clone = div.

How does jQuery clone work?

The . clone() method performs a deep copy of the set of matched elements, meaning that it copies the matched elements as well as all of their descendant elements and text nodes.


1 Answers

Have a look at this: HTML File API

That would probably be the easiest way to do it, e.g.

<input type="file" id="file">

Then just attach a function to the "onChange" function of the element.

EDIT: Just noticed that you're using jQuery, so you could really just do:

$("#file").change(function() { selectFileClick(); });
like image 126
phenomnomnominal Avatar answered Oct 25 '22 04:10

phenomnomnominal