简体   繁体   English

如何使用react-dropzone读取文件内容?

[英]How can I read file content with react-dropzone?

I'm trying to learn more about working with files for an upcoming project. 我正在尝试了解有关为即将到来的项目使用文件的更多信息。 Is there a way with react-dropzone that I can run a function when onDrop happens to change the contents of the files I'm uploading? 当onDrop碰巧更改要上传的文件的内容时,react-dropzone是否可以运行函数? For example, if I'm uploading a word doc that says "Hi everybody" how would I change the content to "Hi Dr. Nick"? 例如,如果我上载一个单词文档,说“嗨,大家好”,我如何将内容更改为“嗨,尼克博士”?

I've tried just accessing the data by using the filereader api after before attempting to make the changes. 在尝试进行更改之前,我尝试过使用filereader api访问数据。 I tried using filereader right after the opening curly bracket of the async function and wasn't able to get it to work. 我尝试在异步函数的大括号后立即使用FileReader,但无法使其正常工作。

import React from 'react';
import Dropzone from 'react-dropzone';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';

const FileUpload = ({
    children, disableClick, channelId, mutate, style = {},
}) => (
    <Dropzone
        style={style}
        className="ignore"
        onDrop={async ([file]) => {
            const response = await mutate({
                variables: {
                    channelId,
                    file,
                },
            });
            console.log(response);
        }}
        disableClick={disableClick}
    >
        {children}
    </Dropzone>
);

const createFileMessageMutation = gql`
  mutation($channelId: Int!, $file: File) {
    createMessage(channelId: $channelId, file: $file)
  }
`;

export default graphql(createFileMessageMutation)(FileUpload);

Any thoughts on how to access and modify the contents of the file are greatly appreciated! 非常感谢您对如何访问和修改文件内容的任何想法! Thanks. 谢谢。

onDrop={async ([file]) => {
  var reader = new FileReader();
  reader.onload = function(e) {
    var contents = e.target.result;
    displayContents(contents);
  };
  reader.readAsText(file);
}}

What you can do on frontend is to read file content and display it, all manipulations with file content should be done on server side. 您可以在前端执行的操作是读取并显示文件内容,所有与文件内容有关的操作都应在服务器端进行。 Push your file using ajax to your server with list of informations you want to change and use for example in nodeJS fs = require('fs') to make file modifications. 使用ajax将文件与要更改的信息列表一起推送到服务器,并在例如nodeJS中使用fs = require('fs')进行文件修改。

Also please visit this post i see that guys provided some solutions for your problem: 另外,请访问这篇文章,我看到人们为您的问题提供了一些解决方案:

Is it possible to write data to file using only JavaScript? 是否可以仅使用JavaScript将数据写入文件?

and here is a fidlle taken from above link: http://jsfiddle.net/o4rhg1xe/1/ 这是从上面的链接中提取的一个文件: http : //jsfiddle.net/o4rhg1xe/1/

In Summary, you can read text file content using code i have provided and then use method from link to create Blob object with content you want and such file can be downlaoded by browser, (see fiddle) 在摘要中,您可以使用我提供的代码读取文本文件的内容,然后使用链接中的方法创建具有所需内容的Blob对象,并且此类文件可以由浏览器下载(请参见小提琴)

Still in my opinion file modifications shoudl be moved to back-end side, ans i cant see no usecase to make them on frontend side. 我仍然认为文件修改应该移到后端,并且我看不到要在前端进行使用的用例。

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

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