繁体   English   中英

使用 javascript 将文件保存在我的项目文件夹中

[英]Use javascript to save file in my project folder

我对 reactJS 非常陌生,我正在尝试创建一个简单的 Web 应用程序,它允许我上传文件,然后将该文件保存到我的项目目录中。

我已经尝试过 browserify-fs,但是当我使用 fs.writeFile 时它似乎没有创建文件

下面的代码允许我上传文件,但我正在努力将文件保存在我的项目目录中

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将数据存储在浏览器中(我假设使用本地存储,但我找不到明确的说明)。

如果要将数据存储在服务器上,则需要:

  1. 将数据发送到服务器(使用 Ajax)
  2. 使用服务器端代码存储该数据

使用以下函数...

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();
}

...并按如下方式调用...

 download('Text to Save to the File', 'TestSave.txt', 'text/plain')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM