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" />
user3110655
  • 121
  • 3
  • 20
  • As i know you couldnt , you should use ajaxForm from jquery http://malsup.com/jquery/form/ – Muhammet Arslan Aug 04 '14 at 09: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! – Quentin Aug 04 '14 at 10:07
  • The normal API to use would be XMLHttpRequest It is well documented here https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest Its been around for a while no major changes in it. – Wayne Aug 04 '14 at 10:15

1 Answers1

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
    }
});
Vlad Cazacu
  • 1,520
  • 12
  • 12
  • Please [avoid link only answers](http://meta.stackoverflow.com/tags/link-only-answers/info). Answers that are "barely more than a link to an external site” [may be deleted](http://stackoverflow.com/help/deleted-answers). – Quentin Aug 04 '14 at 10:08
  • You're right, should have known better. I edited the answer to add the necessary code. – Vlad Cazacu Aug 04 '14 at 10:12
  • The question says "without any third party JS" but you are using jQuery. – Quentin Aug 04 '14 at 10:12
  • The post is tagged jquery. As far as i understood he didn't want to use any third party plugin other than jquery. – Vlad Cazacu Aug 04 '14 at 10:14