我正在將音頻檔案上傳到 Starlette 服務器,并嘗試按照他們在檔案中推薦的方式訪問它,但它給了我一個不可呼叫的錯誤。我收集到問題是呼叫.form()物件request,但我不確定如何讀取它。
服務器路由:
@app.route('/api/upload_track/', methods=['POST'])
async def separate_vocals(request):
audio_data = await request.form()
separator = Separator('spleeter:2stems')
audio_bytes = await (audio_data['file'].read())
return audio_data
客戶:
function FileUploadSingle() {
const [file, setFile] = useState([]);
const handleFileChange = (e) => {
if (e.target.files) {
setFile(e.target.files[0]);
}
};
let audioData = new FormData();
const blob = new Blob([file], {type: 'audio/mpeg'});
audioData.append('file', file, 'file');
console.log(file);
console.log(audioData.get('file'));
// ?? Uploading the file using the fetch API to the server
fetch(`${RESTAPI_URL}/api/upload_track/`, {
method: 'POST',
body: audioData,
})
.then((res) => res.json())
.then((data) => console.log(data))
.catch((err) => console.error(err));
};
}
export default FileUploadSingle;
uj5u.com熱心網友回復:
出現以下錯誤:
TypeError: 'FormData' object is not callable
是在回傳FormData您通過使用await request.form(), 從您的端點(即return audio_data)獲得的物件時引起的——我確信它不是您首先想要回傳的物件,而是audio_bytes. 現在,如果您嘗試使用 回傳位元組陣列return audio_bytes,您將得到:
TypeError: 'bytes' object is not callable
在 Starlette 中,您可以使用Response類來回傳bytes,例如:
from starlette.responses import Response
@app.route('/upload', methods=['POST'])
async def upload(request):
form = await request.form()
contents = await form['file'].read()
return Response(contents)
該類Response還允許您使用引數指定MIME 型別(也稱為媒體型別)media_type。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/533710.html
