[英]React/JS Read File Contents from sources
I have a react application, created via: create-react-app 我有一个通过以下方式创建的React应用程序:create-react-app
I have a PDF file in my sources, which I can import into my react component, via: 我的来源中有一个PDF文件,可以通过以下方式将其导入到我的react组件中:
import pdf from "./Sample.pdf";
After importing the above file, if in my react component's render method, if I do a: 导入以上文件后,如果在我的react组件的render方法中,请执行以下操作:
render() {console.log(pdf, {pdf}); return ...... }
I get the proper file path printed in the console, such as: 我在控制台中打印了正确的文件路径,例如:
/static/media/Sample.ab029b91.pdf {pdf: "/static/media/Sample.ab029b91.pdf"}
Now, I want to base64 encode the contents of this pdf file (to send to a different HTTP server). 现在,我想对这个pdf文件的内容进行base64编码(以发送到其他HTTP服务器)。 How do I achieve this ? 我该如何实现? I know to make a http call or adding a parameter, all I want is to read the PDF file contents and base64 encode it into a JS variable. 我知道要进行http调用或添加参数,我只想读取PDF文件的内容并将base64编码为JS变量。 I think that I may have to use some kind of FileReader but I am not able to figure this out. 我认为我可能必须使用某种FileReader,但我无法弄清楚。 Any help ? 有什么帮助吗?
I cannot use any third party react component for this btw. 我不能为此第三方使用任何第三方反应组件。
You can use url-loader -- https://github.com/webpack-contrib/url-loader . 您可以使用url-loader- https://github.com/webpack-contrib/url-loader 。 This is similar to file loader but with support for DataURL 这类似于文件加载器,但支持DataURL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.