# get-file-object-from-local-path

> Create a JS File object instance from a local path

Latest version **1.0.2** (published 2021-02-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install get-file-object-from-local-path
pnpm add get-file-object-from-local-path
yarn add get-file-object-from-local-path
bun add get-file-object-from-local-path
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2021-02-23 |
| First published | 2021-02-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 5.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17 |
| Author | Alex Browne |
| Maintainers | abrwn |
| Keywords | node, electron, fs, file, buffer, arrayBuffer |

## Links

- npm: https://www.npmjs.com/package/get-file-object-from-local-path
- Repository: https://github.com/abrwn/get-file-object-from-local-path
- Homepage: https://github.com/abrwn/get-file-object-from-local-path#readme
- Issues: https://github.com/abrwn/get-file-object-from-local-path/issues
- npm.io page: https://npm.io/package/get-file-object-from-local-path

## Dependencies (1)

- [mime-types](https://npm.io/package/mime-types.md) ^2.1.28

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2021-02-23
- 1.0.1 — 2021-02-18
- 1.0.0 — 2021-02-18

## README

# get-file-object-from-local-path

A utility to get a JS file object from a local file system path, when using Node together with a frontend client.

This solves the lack of interoperability between Node's `fs` file system (which the browser doesn't have access to), and the browser's `File` object type, which Node cannot create.

## Install

This is a [Node.js](https://nodejs.org/en/) module available through the
[npm registry](https://www.npmjs.com/). Installation is done using the
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):

```sh
$ npm install get-file-object-from-local-path
```

## How to use

The utility contains two functions:
1. `LocalFileData()`: an object constructor function that takes an absolute path as an argument

Call this within a Node environment, passing an absolute path as a single argument, to construct a LocalFileData object containing all the data required to construct a file object in the frontend client:
- `name`: the name of the original file
- `arrayBuffer`: an arrayBuffer created using the buffer of the original file
- `type`: a MIME type based on the extension of the source file. Returns `undefined` if it can't be deduced.

```
// Within node.js
const fileData = new LocalFileData('path/to/file.txt')
```

2. `constructFileFromLocalFileData()`: a function to convert the provided LocalFileData object into a JS File object. This must be executed in the frontend code, which has access to the `window.File` constructor.

```
// Within browser code
const file = constructFileFromLocalFileData(fileData) // returns a JS File object
```

Once created, this file behaves the same way as a JS File object created using an HTML file picker, for example.

## Use cases

`get-file-object-from-local-path` was created to specify files to process and then send from the main thread to the renderer thread in Electron; however, a LocalFileData instance is a plain object that can be serialized/stringified and transmitted in any way required.

Example workflow:
1. Client sends the path of a file to the Node.js environment
2. Backend retrieves file, processes it and creates a LocalFileData object of the result
3. Node.js environment sends LocalFileData object to client
4. Client converts LocalFileData object to a File object for e.g. upload to server

## Limitations

Please note that although any file can be converted into a LocalFileData object, the entire file is loaded into memory when serializing (which will be required as a general rule when sending using Electron's ipcRenderer or as an http request).

**Files of >1GB will therefore cause the process to crash if serialization is required**. This package is intended for transmitting smaller pieces of data and is not suitable for e.g. large videos.

## License

[MIT](LICENSE)

---
_Source: https://npm.io/package/get-file-object-from-local-path · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
