简体   繁体   English

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

[英]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:如果要将数据存储在服务器上,则需要:

  1. Send the data to the server (using Ajax)将数据发送到服务器(使用 Ajax)
  2. Store that data using server-side code使用服务器端代码存储该数据

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.

相关问题 如何打开Javascript对象中我的项目文件夹中的transform xml文件 - how open transform xml file that is in my project folder in Javascript Object 生成的pdf文件保存在项目文件夹中 - Generated pdf file save inside the project folder Highcharts使用JavaScript将图表保存在项目文件夹中,而不是将下载文件夹中保存 - Highcharts save a chart in the project folder instead of downloads folder using javascript 如何使用纯javascript或jQuery将我的HTML5画布屏幕作为png图像保存到我项目的特定文件夹中? - How can I save my HTML5 canvas screen as a png image to a specific folder of my project with only pure javascript or jQuery? JavaScript自动将图像文件保存到文件夹 - JavaScript Automatically Save Image File to Folder 使用CGI和Javascript保存文件 - Use CGI and Javascript to save a file 如何读取项目文件夹 javascript 中的文件? - How to read a file in project folder javascript? 如何从 JavaScript 文件中的项目文件夹加载图像? - How do I load an image from my project folder inside a JavaScript file? 在Javascript中,当在本地工作时,如何从项目文件夹中读取文件以将其用作ArrayBuffer? - In Javascript, when working locally, how to read file from project folder to use it as ArrayBuffer? 如何将html2canvas图像保存到我的项目图像文件夹中? - How to save html2canvas image to my project images folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM