gpt4 book ai didi

javascript - 使用 multer 将音频文件上传到 express js 服务器

转载 作者:行者123 更新时间:2023-12-05 04:21:12 28 4
gpt4 key购买 nike

我正在尝试使用 express js 和 multer 录制然后将录制的音频保存在我的本地目录(上传文件夹)中。第一部分是使用 mic-recorder-to-mp3 录制音频,但我卡在了第二部分,即在单击按钮时保存音频文件。如果我做错了什么,请纠正我。这是 React JS 文件中的代码:

const Mp3Recorder = new MicRecorder({ bitRate: 128 });

class Record extends React.Component {
constructor(props) {
super(props);
this.state = {
isRecording: false,
blobURL: "",
isBlocked: false,
};
}

start = () => {
if (this.state.isBlocked) {
console.log("Permission Denied");
} else {
Mp3Recorder.start()
.then(() => {
this.setState({ isRecording: true });
})
.catch((e) => console.error(e));
}
};

stop = () => {
Mp3Recorder.stop()
.getMp3()
.then(([buffer, blob]) => {
const blobURL = URL.createObjectURL(blob);
this.setState({ blobURL, isRecording: false });
})
.catch((e) => console.log(e));
};

componentDidMount() {
navigator.getUserMedia(
{ audio: true },
() => {
console.log("Permission Granted");
this.setState({ isBlocked: false });
},
() => {
console.log("Permission Denied");
this.setState({ isBlocked: true });
}
);
}

// this function being called on save button
Save = (e) => {
e.preventDefault();
const url = "http://localhost:8000/record";
const data = new FormData();
data.append("audio", this.state.blobURL);

axios.post(url, data).then((e) => {
console.log("success");
});

alert("audio uploaded successfully");
};

render() {
return (
<div className="big-wrapper light">
<div className="container">
<Navbar />
<br />
<br />
<br />
<div className="cont1">
<h2 style={{ color: "white", marginLeft: "-98px" }}>
Remove Noise From Your Audio
</h2>
<br />
<Button
className="bg-transparent border btn record-button rounded-circle shadow-sm text-center"
id="recordButton"
onClick={() => {
this.state.isRecording ? this.stop() : this.start();
}}
>
{this.state.isRecording ? <img src={stop} /> : <img src={mic} />}
</Button>
<br />
<br />
<audio
src={this.state.blobURL}
controls="controls"
autoPlay
id="audio-element"
/>
<br />
<br />
<form
method="post"
action="#"
id="#"
onSubmit={this.Save}
className="form-group"
>
<button className="btn-recordaudio">Save</button>
</form>
</div>
</div>
</div>
);
}
}

export default Record;

这是我的 server.js 代码,其中我有一个从客户端发送的发布请求。

app.use(cors());
app.use(express.static("uploads"));

const storage = multer.diskStorage({
destination(req, file, cb) {
// directory to save the audio
cb(null, "uploads/");
},
filename(req, file, cb) {
const fileNameArr = file.originalname.split(".");
// file name
cb(null, `recording.${fileNameArr[fileNameArr.length - 1]}`);
},
});

const upload = multer({ storage });

app.post("/record", upload.single("audio"), (req, res) =>
res.json({
success: true,
})
);

app.listen(8000, () => {
console.log("server is running");
});

最佳答案

你需要上传blob,而不是blobUrl尝试将 blob 添加到状态,然后将其附加到表单(保持 blobUrl 用于渲染):

 this.state = {
blob: null
};
//...

this.setState({ blob});

//...
data.append("audio", this.state.blob, 'mp3.mp3');

编辑

您可以从缓冲区创建一个文件,然后附加它以防您需要向该文件添加更多元数据:

this.state = {
buffer: null
};


const file = new File(this.state.buffer, 'mp3.mp3', {
type: this.state.blob.type
});


data.append("audio", file);

关于javascript - 使用 multer 将音频文件上传到 express js 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74294464/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com