0

i just have input type file and want to move csv file without any third party js plugin ?

<input type="file" name="file" id="file" />
<input type="button" name="upload" id="upload" value="Upload" />
3
  • As i know you couldnt , you should use ajaxForm from jquery malsup.com/jquery/form Commented Aug 4, 2014 at 9:54
  • @MuhammetArslan — What? That's nonsense. If it isn't possible to write code to do it without a 3rd party library, then the 3rd party wouldn't be able to write the library to do it! Commented Aug 4, 2014 at 10:07
  • The normal API to use would be XMLHttpRequest It is well documented here developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/… Its been around for a while no major changes in it. Commented Aug 4, 2014 at 10:15

1 Answer 1

0

You can use the FormData interface. Note that this doesn't work on IE lower than 10.

Here's a great tutorial on how to use it: http://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax

In short, you have to bind a change event on the file input to capture the selected file and on the form submit you'd create a new FormData object to which you'd append the file data previously saved.

// Variable to store your files
var files;

// Add events
$('input[type=file]').on('change', prepareUpload);

// Grab the files and set them to our variable
function prepareUpload(event)
{
  files = event.target.files;
}

On form submit:

var data = new FormData();
$.each(files, function(key, value)
{
    data.append(key, value);
});

$.ajax({
    url: 'submit.php?files',
    type: 'POST',
    data: data,
    cache: false,
    dataType: 'json',
    processData: false, // Don't process the files
    contentType: false, // Set content type to false as jQuery will tell the server its a query string request
    success: function(data, textStatus, jqXHR)
    {
        if(typeof data.error === 'undefined')
        {
            // Success so call function to process the form
            submitForm(event, data);
        }
        else
        {
            // Handle errors here
            console.log('ERRORS: ' + data.error);
        }
    },
    error: function(jqXHR, textStatus, errorThrown)
    {
        // Handle errors here
        console.log('ERRORS: ' + textStatus);
        // STOP LOADING SPINNER
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

Please avoid link only answers. Answers that are "barely more than a link to an external site” may be deleted.
You're right, should have known better. I edited the answer to add the necessary code.
The question says "without any third party JS" but you are using jQuery.
The post is tagged jquery. As far as i understood he didn't want to use any third party plugin other than jquery.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.