使用客户端clojurescript/re-frame应用程序读入文件 [英] Read in file with client-side clojurescript/re-frame app

查看:75
本文介绍了使用客户端clojurescript/re-frame应用程序读入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个客户端应用程序,该应用程序应读取文件,转换其内容然后导出结果.为此,我决定重新框架.

I'm writing a client-side application which should read in a file, transform its content and then export the result. To do this, I decided on Re-Frame.

现在,我刚刚开始将脑袋围绕在Re-Frame上,并进行cloujurescipt本身,并且可以完成以下工作:

Now, I'm just starting to wrap my head around Re-Frame and cloujurescipt itself and got the following thing to work:

在我的视图函数中的任何地方,只要通过简单的HTML输入选择了新文件,我都会发送此消息.

Somewhere in my view functions, I send this whenever a new file gets selected via a simple HTML input.

[:input {:class "file-input" :type "file"                                 
         :on-change #(re-frame/dispatch                                   
           [::events/file-name-change (-> % .-target .-value)])}]

我得到的是类似 C:\ fakepath \ file-name.txt 的东西,而falsepath实际上是其中的一部分.

What I get is something like C:\fakepath\file-name.txt, with fakepath actually being part of it.

我的事件处理程序当前仅拆分名称,并保存我在上面输入的内容所订阅的文件名,以显示所选文件.

My event handler currently only splits the name and saves the file name to which my input above is subscribed to display the selected file.

(re-frame/reg-event-db                       
  ::file-name-change                       
   (fn [db [_ new-name]]                          
     (assoc db :file-name (last (split new-name #"\\"))))) 

此外,我想读取文件以稍后在本地进行处理.假设我只是更改自己的 on-change 操作和事件处理程序以执行此操作,那我该怎么做?

Additionally I want to read in the file to later process it locally. Assuming I'd just change my on-change action and the event handler to do this instead, how would I do it?

我搜索了一段时间,但什么也没找到.唯一出现在其他框架之类的地方,但我不想为每个新问题引入新的依赖关系.

I've searched for a while but found next to nothing. The only things that came up where other frameworks and such, but I don't want to introduce a new dependency for each and every new problem.

推荐答案

我假设您想使用HTML5 API在客户端中完成所有操作(例如,没有实际上传到服务器).

I'm assuming you want to do everything in the client using HTML5 APIs (eg. no actual upload to a server).

MDN的本指南可能会派上用场: https://developer.mozilla.org/zh-CN/docs/Web/API/File/Using_files_from_web_applications

This guide from MDN may come handy: https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications

似乎您可以订阅用户选择文件时触发的事件,然后可以获得所述文件的列表,并通过File API检查文件内容:

It seems you can subscribe to the event triggered when the user selects the file(s), then you can obtain a list of said files, and inspect the files contents through the File API: https://developer.mozilla.org/en-US/docs/Web/API/File

对于您而言,您需要将事件中对 FileList 对象的引用保存在某个地方,并在以后重新使用.

In your case, you'll need to save a reference to the FileList object from the event somewhere, and re-use it later.

这篇关于使用客户端clojurescript/re-frame应用程序读入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆