$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - send blob to python flask and then save it - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - send blob to python flask and then save it - Stack Overflow

matteradmin16PV0评论

So I'm trying to make a website that record your voice, the problem is that when I send to a flask server the blob file or the blob url, my flask python code says that is no content while it is, how can I send the blob, so the server can save it as a file.

    mediaRecorder.addEventListener("stop", () => {
      const audioBlob = new Blob(audioChunks, { type: "audio/wav" })
      const audioUrl = URL.createObjectURL(audioBlob);
      const audio = new Audio(audioUrl);
      audio.play();


      var data = new FormData()
      data.append('file', audioUrl)

      fetch('http://127.0.0.1:5000/receive', {
          method: 'POST',
          body: data

      }).then(response => response.json()
      ).then(json => {
          console.log(json)
      });

and my python flask code:

@app.route("/receive", methods=['post'])
def form():
    files = request.files
    file = files.get('file')
    print(file)

    with open(os.path.abspath(f'backend/audios/{file}'), 'wb') as f:
        f.write(file.content)

    response = jsonify("File received and saved!")
    response.headers.add('Access-Control-Allow-Origin', '*')

    return response

is there a way to do it? send record blob file, download it into python?

So I'm trying to make a website that record your voice, the problem is that when I send to a flask server the blob file or the blob url, my flask python code says that is no content while it is, how can I send the blob, so the server can save it as a file.

    mediaRecorder.addEventListener("stop", () => {
      const audioBlob = new Blob(audioChunks, { type: "audio/wav" })
      const audioUrl = URL.createObjectURL(audioBlob);
      const audio = new Audio(audioUrl);
      audio.play();


      var data = new FormData()
      data.append('file', audioUrl)

      fetch('http://127.0.0.1:5000/receive', {
          method: 'POST',
          body: data

      }).then(response => response.json()
      ).then(json => {
          console.log(json)
      });

and my python flask code:

@app.route("/receive", methods=['post'])
def form():
    files = request.files
    file = files.get('file')
    print(file)

    with open(os.path.abspath(f'backend/audios/{file}'), 'wb') as f:
        f.write(file.content)

    response = jsonify("File received and saved!")
    response.headers.add('Access-Control-Allow-Origin', '*')

    return response

is there a way to do it? send record blob file, download it into python?

Share Improve this question edited Jan 16, 2022 at 21:27 davidism 128k31 gold badges415 silver badges347 bronze badges asked Jan 16, 2022 at 19:35 Bernardo OlisanBernardo Olisan 6759 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

The problem is in this line:

data.append('file', audioUrl)

you don't use FormData.append the right way. it should be:

data.append('file', audioBlob , 'file')

See documentation: https://developer.mozilla/en-US/docs/Web/API/FormData/append

Post a comment

comment list (0)

  1. No comments so far