sohotcall

Ajax Upload

Apr 2nd, 2020
873
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const formData = new FormData();
  2.  
  3. formData.append("username", "Groucho");
  4. formData.append("accountnum", 123456);
  5.  
  6. const fileInputElement = document.getElementById( 'myFile' );
  7. formData.append( fileInputElement.name, fileInputElement.files[0], fileInputElement.files[0].name );
  8. formData.append("userfile", fileInputElement.files[0]);
  9. formData.append("webmasterfile", new Blob(['<a><b>hey!</b></a>'], { type: "text/xml"}), "data.xml");
  10.  
  11. const xhr = new XMLHttpRequest();
  12. xhr.open("POST", "http://foo.com/submitform.php");
  13. xhr.onload = function(){
  14.     if ( xhr.status >= 200 && xhr.status < 400 )
  15.         console.log( "Response: " + xhr.responseText );
  16. };
  17. xhr.onerror = function(){
  18.     console.log( "Failed" );
  19. };
  20. xhr.send(formData);
RAW Paste Data