Giter Site home page Giter Site logo

md-asikuzzaman / next-file-64ify Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 62 KB

A package for handling base64 encoding and decoding in React.js/Next.js applications

Home Page: https://www.npmjs.com/package/next-file-64ify

License: MIT License

TypeScript 100.00%
file-upload html-files next-file-64ify tsup typescript nextjs

next-file-64ify's Introduction

next-file-64ify ๐Ÿ“

The "next-file-64ify" package simplifies file handling in Next.js/React.js applications by converting selected files to base64 strings. It provides easy-to-use functions for validating file types and sizes, ensuring smooth and efficient file uploads. It is ideal for image files and integrates seamlessly with React components for streamlined file management.

Installation:

GitHub package.json version

npm i next-file-64ify
# or
yarn add next-file-64ify
# or
pnpm i next-file-64ify

โœ” Simply connect with your Next.js or React.js application ๐Ÿค.

"use client";

import { useState, useRef } from "react";
import { _64ify } from "next-file-64ify"; // Import _64ify package

const MyFile = () => {
 const [myFile, setMyFile] = useState<File | null>(null);
 const inputRef = useRef<HTMLInputElement>(null);

 // Function to handle form submission
 const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
   e.preventDefault();
   
   if (myFile) {
     try {
       const { data, isLoading, isError, isValidSize } = await _64ify(myFile, {
         allowedSizes: {
           minSize: 10, // file size in KB
           maxSize: 1024 * 2, // file size in MB
         },
         allowedTypes: ["image/jpeg", "image/png"], // Allowed file types
       });

       console.log({ data, isLoading, isError, isValidSize }); // Log results

       // Reset file input field and state
       if (inputRef.current) {
         inputRef.current.value = "";
         setMyFile(null);
       }
     } catch (error: unknown) {
       console.error("Error processing file:", error); // Log any errors
     }
   }
 };

 return (
   <form onSubmit={handleSubmit}>
     <input
       type="file"
       ref={inputRef} // Reference to file input element
       accept="image/jpeg, image/png" // Restrict file selection
       onChange={
         (e: React.ChangeEvent<HTMLInputElement>) =>
           e.target.files && setMyFile(e.target.files[0])
       } // Update state with selected file
     />
     <button type="submit">Upload File</button>
   </form>
 );
};

export default MyFile;

โœ” Follow the guide to set a specific file (Sizes).

const { data, isLoading, isError, isValidSize } = await _64ify(myFile, {
 allowedSizes: {
  minSize: 1024, // file size in KB
  maxSize: 1024 * 5, // file size in MB
 },
 ...
});

โœ” 1. Add specific file types inside the array (for the package)

const { data, isLoading, isError, isValidSize } = await _64ify(myFile, {
 allowedTypes: ["image/jpeg", "image/png", "image/webp"],
 ...
});

โœ” 2. Add specific file types here (for the browser)

<input type="file" accept="image/jpeg, image/png, "image/webp", ..."/>

โœ” Most commonly used file types.

// Copy and paste what you need ๐Ÿ˜Š
{
  "image/jpeg",
  "image/png",
  "image/svg+xml",
  "image/gif",
  "image/webp",
  "image/bmp",
  "image/tiff",
  "image/x-icon",
  "image/vnd.microsoft.icon",
  "image/vnd.wap.wbmp",
  "image/heic",
  "image/heif",
  "image/jxr",
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.