javascript:发送带有音频文件的POST,然后重定向到新页面

javascript: send POST with audio file and then redirect to a new page

本文关键字:POST 然后 重定向 新页面 文件 音频 javascript      更新时间:2023-09-26

这是我所拥有的。

/上传>将现有的本地音频文件上传到/post_receiver通过post请求

/post_receiver>接收post请求并响应HTML页面

/record>录制要下载到本地或上载到的音频文件/post_receiver通过AJAX post请求

它们运行良好,但我不想使用AJAX。我更喜欢一个新的HTML页面。我知道如何通过document.createElement("form")、document.body.appendChild()和submit()使用传统表单。但是,它似乎无法像FormData那样保存音频文件。顺便说一下,我的服务器是AmazonEC2上的node.js,但我认为它没有关联。

来自的非AJAX POST/记录

<form method="POST" action="/audio" enctype="multipart/form-data">
<input type="file" name="audio" value="blob:address" filename="audio.wav">
</form>
    Cannot read property 'filename' of undefined
    TypeError: Cannot read property 'filename' of undefined
        at router.get.html (./routes/index.js:11:74)
        at Layer.handle [as handle_request] (./node_modules/express/lib/router/layer.js:95:5)
        at next (./node_modules/express/lib/router/route.js:131:13)
        at Object.<anonymous> (./node_modules/multer/lib/make-middleware.js:52:37)
        at Object.immediate._onImmediate (timers.js:348:16)
        at processImmediate [as _immediateCallback] (timers.js:330:15)
------WebKitFormBoundaryxh3BZE3s5vvxVQ1n
Content-Disposition: form-data; name="audio"; filename=""
Content-Type: application/octet-stream

------WebKitFormBoundaryxh3BZE3s5vvxVQ1n--

AJAX POST来自/记录或POST/上传

<form action="/audio" method="post" enctype="multipart/form-data">
<input type="file" name="audio" required="">
<button type="submit">Submit</button>
</form>
------WebKitFormBoundary47iCp5HAMwe089e4
Content-Disposition: form-data; name="audio"; filename="audio.wav"
Content-Type: audio/wav

------WebKitFormBoundary47iCp5HAMwe089e4--

下面的技巧有效,但它仍然在同一页上,这不是我想要的。

document.open();
document.write(<XMLHttpRequest>.responseText);
document.close();

另外,像socket/stream/session/cookies这样的东西有点过头了。我只是想保持简单,我希望有一种方法可以将FormData作为请求发送,并让浏览器直接处理来自服务器的响应。如果这是不可能的,那么请让我知道一个简单的方法来实现这一点。

感谢任何想法和建议。非常感谢。

我遇到了同样的问题,这就是我最终要做的。我将POST处理程序从返回重定向更改为在正文中返回带有重定向URL的响应。然后我就这样做了(而不是写文档技巧):

redirectURL = <XMLHttpRequest>.responseText;
window.location = redirectURL;