Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. 'use strict';
  2.  
  3. // HTML contains element 'message'. This is used to show the server's response
  4. // Select it and save it as a variable/object
  5.  
  6. // make function 'upload' which
  7. // - prevents the form from sending
  8. // - writes 'Upload in progress...' into 'message' element
  9. // - selects the file input field
  10. // - makes FormData -object and adds the file selected byt the user into the object
  11. // - send the file to the same url as in task a by using fetch -method
  12. // - when file upload is complete, writes server response to 'message' element
  13. // function ends
  14.  
  15. // make an event listener which calls upload function when the form is submitted
  16.  
  17. const upload = () => {
  18. const message = document.getElementById('message');
  19. message.innerHTML = 'Upload in progress...'
  20.  
  21. const input = document.querySelector('input[type="file"]');
  22. const data = new FormData();
  23.  
  24. data.append('fileup', input.files[0]);
  25. const settings = {
  26. method: 'POST',
  27. body: data
  28. };
  29. console.log(settings)
  30.  
  31. fetch('http://10.114.32.118:8080/photoUploadTask5/upload', settings)
  32. .then((response) => {
  33. return response.text();
  34. }).then((text) => {
  35. message.innerHTML = text
  36. }).catch((err) => {
  37. console.log(err)
  38. message.innerHTML = "an error has occured"
  39. });
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement