Skip to content

Releases: Prokken/file-upload-with-mongodb

GridFS File Upload System with MongoDB

14 Sep 21:22
Compare
Choose a tag to compare

MongoDB File Upload API Documentation

This API allows users to upload, download, rename, and delete files in MongoDB using GridFS. It supports single and multiple file uploads, as well as various download formats, including zipped files and base64 encoding.

Base URL

<your_base_url>


Endpoints

1. Upload a File

Uploads a single file to MongoDB using GridFS.

  • URL: /upload/file
  • Method: POST
  • Headers:
    • Content-Type: multipart/form-data
  • Body:
    • file: A file input with the file to be uploaded.
  • Example Request:
    • Body:
      form-data
      Key: file
      Value: <file_path>/whatsapp-cloud-api.png
      
  • Success Response:
    • Status: 200 OK
    • Body:
      {
        "message": "File uploaded successfully",
        "fileId": "66e5f0563d20ffd1705bb1eb"
      }
  • Error Response:
    • Status: 400 Bad Request
    • Body:
      {
        "error": "Invalid file format or missing file"
      }

2. Upload Multiple Files

Uploads multiple files to MongoDB using GridFS.

  • URL: /upload/files
  • Method: POST
  • Headers:
    • Content-Type: multipart/form-data
  • Body:
    • files: Multiple files input.
  • Example Request:
    • Body:
      form-data
      Key: files
      Value: <file_path>/graphics-designer.png, <file_path>/whatsapp-cloud-api.png
      
  • Success Response:
    • Status: 200 OK
    • Body:
      {
        "message": "Files uploaded successfully",
        "fileIds": ["66e5f0563d20ffd1705bb1eb", "66e5f0563d20ffd1705bb1ec"]
      }
  • Error Response:
    • Status: 400 Bad Request
    • Body:
      {
        "error": "Invalid files format or missing files"
      }

3. Download a File

Downloads a single file from MongoDB using its file ID.

  • URL: /download/files/:fileId
  • Method: GET
  • Path Variables:
    • fileId: The ID of the file in MongoDB.
  • Example Request:
    • URL: /download/files/66e5f0563d20ffd1705bb1eb
  • Success Response:
    • Status: 200 OK
    • Headers:
      • Content-Disposition: attachment; filename=<filename>
    • Body: File content.
  • Error Response:
    • Status: 404 Not Found
    • Body:
      {
        "error": "File not found"
      }

4. Download Multiple Files as Zip

Downloads multiple files from MongoDB in a zipped format.

  • URL: /download/files-zip
  • Method: GET
  • Success Response:
    • Status: 200 OK
    • Headers:
      • Content-Disposition: attachment; filename=files.zip
    • Body: Zipped file content.
  • Error Response:
    • Status: 404 Not Found
    • Body:
      {
        "error": "Files not found or unable to zip"
      }

5. Download Multiple Files as Base64

Downloads multiple files from MongoDB encoded in base64.

  • URL: /download/files-base64
  • Method: GET
  • Success Response:
    • Status: 200 OK
    • Body:
      {
        "files": [
          {
            "filename": "file1.png",
            "content": "<base64_encoded_content>"
          },
          {
            "filename": "file2.png",
            "content": "<base64_encoded_content>"
          }
        ]
      }
  • Error Response:
    • Status: 404 Not Found
    • Body:
      {
        "error": "Files not found"
      }

6. Rename a File

Renames an existing file in MongoDB.

  • URL: /rename/file/:fileId
  • Method: PUT
  • Path Variables:
    • fileId: The ID of the file in MongoDB.
  • Body:
    • Content-Type: application/json
    • JSON:
      {
        "filename": "new-filename.png"
      }
  • Success Response:
    • Status: 200 OK
    • Body:
      {
        "message": "File renamed successfully"
      }
  • Error Response:
    • Status: 404 Not Found
    • Body:
      {
        "error": "File not found"
      }

7. Delete a File

Deletes a file from MongoDB using its file ID.

  • URL: /delete/file/:fileId
  • Method: DELETE
  • Path Variables:
    • fileId: The ID of the file in MongoDB.
  • Example Request:
    • URL: /delete/file/66e5efa3c337b9d59aa5b858
  • Success Response:
    • Status: 200 OK
    • Body:
      {
        "message": "File deleted successfully"
      }
  • Error Response:
    • Status: 404 Not Found
    • Body:
      {
        "error": "File not found"
      }

Ensure to replace <your_base_url> with the actual base URL http://localhost:3000 or number you've ported of your API service. This documentation should provide clear guidance on how to interact with the file upload system using the endpoints provided.

or, Checkout this video demonstration

file-upload-with-mongodb.mp4