[英]Use javascript to save file in my project folder
I am VERY new to reactJS and I am trying to create a simple web app that allows me to upload a file and then save that file to my projects directory.我对 reactJS 非常陌生,我正在尝试创建一个简单的 Web 应用程序,它允许我上传文件,然后将该文件保存到我的项目目录中。
I have tried browserify-fs but its doesn't seem to create the file when I use fs.writeFile我已经尝试过 browserify-fs,但是当我使用 fs.writeFile 时它似乎没有创建文件
The below code allows me to upload a file but I am struggling to save the file in my project directory下面的代码允许我上传文件,但我正在努力将文件保存在我的项目目录中
import ReactDOM from 'react-dom';
import Dropzone from 'react-dropzone';
class App extends Component {
onDrop = (acceptedFiles) => {
// Save acceptedFiles in this scripts directory
}
render() {
return (
<Dropzone onDrop={this.onDrop}>
{({getRootProps, getInputProps}) => (
<div {...getRootProps()}>
<input {...getInputProps()} />
Click me to upload a file!
</div>
)}
</Dropzone>
);
}
}
export default App;
ReactDOM.render(
<App />,
document.getElementById('root')
);
browserify-fs
stores data in the browser (I'm assuming using local storage, but I can't find a clear statement to that effect). browserify-fs
将数据存储在浏览器中(我假设使用本地存储,但我找不到明确的说明)。
If you want to store data on the server then you'll need to:如果要将数据存储在服务器上,则需要:
Use the following function...使用以下函数...
function download(content, fileName, contentType) {
var a = document.createElement("a");
var file = new Blob([content], {type: contentType});
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
...and call as follows... ...并按如下方式调用...
download('Text to Save to the File', 'TestSave.txt', 'text/plain')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.