npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nuxt-file-storage

v0.2.9

Published

Easy solution to store files in your nuxt apps. Be able to upload files from the frontend and recieve them from the backend to then save the files in your project.

Downloads

1,500

Readme

Nuxt File Storage Banner

Nuxt File Storage

Visits Badge npm version npm downloads License Nuxt

Easy solution to store files in your nuxt apps. Be able to upload files from the frontend and recieve them from the backend to then save the files in your project.

Features

  • 📁  Get files from file input and make them ready to send to backend
  • ⚗️  Serialize files in the backend to be able to use them appropriately
  • 🖴  Store files in a specified location in your Nuxt backend with Nitro Engine

Quick Setup

  1. Add nuxt-file-storage dependency to your project
# Using pnpm
pnpm add -D nuxt-file-storage

# Using yarn
yarn add --dev nuxt-file-storage

# Using npm
npm install --save-dev nuxt-file-storage
  1. Add nuxt-file-storage to the modules section of nuxt.config.ts
export default defineNuxtConfig({
	modules: ['nuxt-file-storage'],
})

That's it! You can now use Nuxt Storage in your Nuxt app ✨

Configuration

You can currently configure a single setting of the nuxt-file-storage module. Here is the config interface:

export default defineNuxtConfig({
	modules: ['nuxt-file-storage'],
	fileStorage: {
		// enter the absolute path to the location of your storage
		mount: '/home/$USR/development/nuxt-file-storage/server/files',

		// {OR} use environment variables (recommended)
		mount: process.env.mount
		// you need to set the mount in your .env file at the root of your project
	},
})

Usage

Handling Files in the frontend

You can use Nuxt Storage to get the files from the <input> tag:

<template>
	<input type="file" @input="handleFileInput" />
</template>

<script setup>
	// handleFileInput can handle multiple files
	// clearOldFiles: true by default, each time the user adds files the `files` ref will be cleared
	const { handleFileInput, files } = useFileStorage({ clearOldFiles: false })
</script>

The files return a ref object that contains the files

handleFileInput returns a promise in case you need to check if the file input has concluded

Here's an example of using files to send them to the backend

<template>
	<input type="file" @input="handleFileInput" />
	<button @click="submit">submit</button>
</template>

<script setup>
const { handleFileInput, files } = useFileStorage()

const submit = async () => {
	const response = await $fetch('/api/files', {
		method: 'POST',
		body: {
			files: files.value
		}
	})
}
</script>

Handling multiple file input fields

You have to create a new instance of useFileStorage for each input field

<template>
	<input type="file" @input="handleFileInput" multiple />   ← | 1 |
	<input type="file" @input="profileInputHandler" />                 ← | 2 |
</template>

<script setup>
	const { handleFileInput, files } = useFileStorage()       ← | 1 |

	const {
		handleFileInput: profileInputHandler,
		files: profileImage
	} = useFileStorage()                                               ← | 2 |
</script>

by calling a new useFileStorage instance you seperate the internal logic between the inputs

Handling files in the backend

using Nitro Server Engine, we will make an api route that recieves the files and stores them in the folder userFiles

export default defineEventHandler(async (event) => {
	const { files } = await readBody<{ files: File[] }>(event)

	for ( const file of files ) {
		await storeFileLocally(
			file, 		  // the file object
			8,            // you can add a name for the file or length of Unique ID that will be automatically generated!
			'/userFiles'  // the folder the file will be stored in
		)

		// {OR}

		// Parses a data URL and returns an object with the binary data and the file extension.
		const { binaryString, ext } = parseDataUrl(file.content)
	}

	return 'success!'
})

interface File {
	name: string
	content: string
}

And that's it! Now you can store any file in your nuxt project from the user ✨

Contribution

Run into a problem? Open a new issue. I'll try my best to include all the features requested if it is fitting to the scope of the project.

Want to add some feature? PRs are welcome!

  • Clone this repository
  • install the dependencies
  • prepare the project
  • run dev server
git clone https://github.com/NyllRE/nuxt-file-storage && cd nuxt-file-storage
npm i
npm run dev:prepare
npm run dev