Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. router.post('/crop', upload.fields([{name: 'img'}]), function (req, res, next) {
  2.  
  3. console.log('files', req.files.img);
  4.  
  5. var path = __dirname + '/' + req.files.img[0].path;
  6. console.log(path)
  7. res.sendFile(path);
  8.  
  9. $('#form1').submit(function(e){
  10. var formData = new FormData($(this)[0]);
  11. $.ajax({
  12. type:'POST',
  13. url:'http://localhost:8080/crop',
  14. data : formData,
  15. contentType: false, // <-----------------
  16. processData: false
  17. }).done(function(data){
  18. //print response on success
  19. $("#status").html(data);
  20.  
  21. }).fail(function(data) {
  22. $("#status").html('Error');
  23. });
  24. e.preventDefault();
  25. });
  26.  
  27. res.set('Content-Type', 'image/png');
  28.  
  29. $("#status").html(data);
  30.  
  31. router.get('/img', (req, res) => {
  32. res.end(
  33. `<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  34. <form id='form1'>
  35. <input type='file' />
  36. <button>Send</button>
  37. </form>
  38. <div id='status'></div>
  39. <script>
  40. $('#form1').submit(function(e){
  41. var formData = new FormData($(this)[0]);
  42. $.ajax({
  43. type:'POST',
  44. url:'http://localhost:8080/img',
  45. data : formData,
  46. contentType: false, // <-----------------
  47. processData: false
  48. }).done(function(data){
  49. img = new Image();
  50. img.src = data;
  51. $("#status").html(img);
  52. }).fail(function(data) {
  53. $("#status").html('Error');
  54. });
  55. e.preventDefault();
  56. });
  57. </script>
  58. `);
  59. });
  60.  
  61. router.post('/img', (req, res) => {
  62. const path = __dirname + '/img.png';
  63. const base64Image = new Buffer(fs.readFileSync(path), 'binary').toString('base64');
  64. res.end(`data:image/png;base64,${base64Image}`);
  65. });
  66.  
  67. $('#form1').submit(function(e){
  68. e.preventDefault();
  69. var xhr = new XMLHttpRequest();
  70. xhr.onreadystatechange = function(){
  71. if (this.readyState == 4 && this.status == 200){
  72. var img = document.createElement('img');
  73. var url = window.URL || window.webkitURL;
  74. img.src = url.createObjectURL(this.response);
  75. $('#status').html(img);
  76. }
  77. }
  78. xhr.open('POST', '/img');
  79. xhr.responseType = 'blob';
  80. xhr.send();
  81. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement