Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- router.post('/crop', upload.fields([{name: 'img'}]), function (req, res, next) {
- console.log('files', req.files.img);
- var path = __dirname + '/' + req.files.img[0].path;
- console.log(path)
- res.sendFile(path);
- $('#form1').submit(function(e){
- var formData = new FormData($(this)[0]);
- $.ajax({
- type:'POST',
- url:'http://localhost:8080/crop',
- data : formData,
- contentType: false, // <-----------------
- processData: false
- }).done(function(data){
- //print response on success
- $("#status").html(data);
- }).fail(function(data) {
- $("#status").html('Error');
- });
- e.preventDefault();
- });
- res.set('Content-Type', 'image/png');
- $("#status").html(data);
- router.get('/img', (req, res) => {
- res.end(
- `<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
- <form id='form1'>
- <input type='file' />
- <button>Send</button>
- </form>
- <div id='status'></div>
- <script>
- $('#form1').submit(function(e){
- var formData = new FormData($(this)[0]);
- $.ajax({
- type:'POST',
- url:'http://localhost:8080/img',
- data : formData,
- contentType: false, // <-----------------
- processData: false
- }).done(function(data){
- img = new Image();
- img.src = data;
- $("#status").html(img);
- }).fail(function(data) {
- $("#status").html('Error');
- });
- e.preventDefault();
- });
- </script>
- `);
- });
- router.post('/img', (req, res) => {
- const path = __dirname + '/img.png';
- const base64Image = new Buffer(fs.readFileSync(path), 'binary').toString('base64');
- res.end(`data:image/png;base64,${base64Image}`);
- });
- $('#form1').submit(function(e){
- e.preventDefault();
- var xhr = new XMLHttpRequest();
- xhr.onreadystatechange = function(){
- if (this.readyState == 4 && this.status == 200){
- var img = document.createElement('img');
- var url = window.URL || window.webkitURL;
- img.src = url.createObjectURL(this.response);
- $('#status').html(img);
- }
- }
- xhr.open('POST', '/img');
- xhr.responseType = 'blob';
- xhr.send();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement