[英]React.js: How do I send search query from react.js frontend to express.js backend?
[英]Moving a file from a React.JS frontend to a C++ backend
所以我有一個用 C++ 編寫的 function,它適用於文件。 我可以將它轉換為腳本和腳本以獲取文件位置的參數並對其進行處理,這不是問題。
我的前端是用 React 完成的,它仍然沒有連接到后端。 我有一個用戶單擊的“上傳文件”按鈕,我需要在我的后端有這個文件才能在上面運行 C++ 代碼。
從理論上講,我想到的方式(不確定是否有效):
wget (link that we got from the HTTP request through REST API
And after that the python script runs the C++ function on the file, and returns the output through the HTTP request.
那有意義嗎? 有沒有更簡單或更好的方法?
謝謝
讓用戶加載文件 -> 讓 C++ function 在該文件上運行 -> 將 output 返回給用戶。
正如您在問題中所說,您可以上傳到谷歌驅動器然后 wget 文件,但是它有點太復雜了。 您可以做的是跳過上傳到 Google 驅動器並直接通過 formdata 發送文件。
這是取自此處的 React 中的一個工作示例:
import React from 'react'
import axios, { post } from 'axios';
class SimpleReactFileUpload extends React.Component {
constructor(props) {
super(props);
this.state ={
file:null
}
this.onFormSubmit = this.onFormSubmit.bind(this)
this.onChange = this.onChange.bind(this)
this.fileUpload = this.fileUpload.bind(this)
}
onFormSubmit(e){
e.preventDefault() // Stop form submit
this.fileUpload(this.state.file).then((response)=>{
console.log(response.data);
})
}
onChange(e) {
this.setState({file:e.target.files[0]})
}
fileUpload(file){
const url = 'http://example.com/file-upload';
const formData = new FormData();
formData.append('file',file)
const config = {
headers: {
'content-type': 'multipart/form-data'
}
}
return post(url, formData,config)
}
render() {
return (
<form onSubmit={this.onFormSubmit}>
<h1>File Upload</h1>
<input type="file" onChange={this.onChange} />
<button type="submit">Upload</button>
</form>
)
}
}
對於您的特定用例,我不建議您使用完整的 REST-API,您可以簡單地使用事件驅動的 lambda function,例如AWS Lambda 。 這樣您只需上傳到 Google Drive 或 S3 存儲桶,然后 Lambda 將運行並生成 output 可以返回給用戶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.