Git Product home page Git Product logo

Comments (2)

tnylea avatar tnylea commented on August 26, 2024 1

@utkarshrai003

I used your code as reference in order to pass the file using JSON, and I ran into the problem you had as well and figured out the issue. Take a look at my solution below:

uploadByFile(file) {
                    
    let reader = new FileReader();

    reader.onload = function (e) {
        
        return fetch('/uploadFile', {
            method: 'post',
            body: JSON.stringify({
                file: e.target.result,
                filename: 'first_img'
            })
        }).then(function(response) {
            return {
                success: 1,
                file: {
                    url: 'https://my-photo-gallary.s3.ap-south-1.amazonaws.com/go_that_extra_mile.png'
                }
            }
        })

    };
    
    // Read the file
    reader.readAsDataURL(file);
}

You needed to use FileReader(), and then load the image which is sent to the server in your JSON object, passing it as a base64 image.

You can then use fs.writeFile or fs.writeFileSync to save the image where you want.

Hope that helps :)

from image.

Arstman avatar Arstman commented on August 26, 2024

I had tried the way @tnylea mentioned, but didn't work due to my server (s3 compliance) response that put file as base64 in json is a "Bad Request", so i use another solution, just use FormData, and should be acceptable by most servers.

...
  uploadByFile(file) {

                   let fd = new FormData();
                   fd.append('file',file);

                   return fetch('/uploadFile', {
                           method:'POST',
                           body:fd
                      }).then(res=>res.json())
                      .then(response => {
                               return {
                                   success: 1,
                                   file: {
                                          url: 'https://your-url/'+response.your_file_uri
                                   }
                         }
                    })
     }

...

from image.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.