Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

92
Vistas
How to send file from Nodejs to Flask Python?

Hope you are doing well. I'm trying to send pdfs file from Nodejs to Flask using Axios. I read files from a directory (in the form of buffer array) and add them into formData (an npm package) and send an Axios request.

 const existingFile = fs.readFileSync(path)                                
    console.log(existingFile)
    const formData = new nodeFormData()
    formData.append("file", existingFile)
    formData.append("fileName", documentData.docuName)
    try {
         const getFile = await axios.post("http://127.0.0.1:5000/pdf-slicer", formData,
                        {
                          headers: {
                                      ...formData.getHeaders()
                                   }
                           })
         console.log(getFile)} catch (e) {console.log(e, "getFileError")}

On flask side:

I'm trying to get data from the request.

    print(request.files)
    
    if (request.method == "POST"):
        file=request.form["file"]
        if file:
            print(file)

in request.file, I'm getting ImmutableMultiDict([])

but in request.form["file"], I'm getting data something like this:

request.form["file"] print example

how can I handle this type of file format or how can I convert this file format to python fileObject.

11 months ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I solved this issue by updating my Nodejs code. We need to convert formData file into octet/stream format.

so I did minor change in my formData code :

before: formData.append("file", existingFile)

after: formData.append("file", fs.createReadStream(existingFile)

Note: fs.createReadStream only accepts string or uint8array without null bytes. we cannot pass the buffer array.

11 months ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.