I'll provide you with a complete code example for a Next.js project using TypeScript and Tailwind CSS that allows users to upload an image to Hostinger Cloud and immediately get the image link. We'll use the Hostinger API to handle the image upload.
First, let's create the necessary components and API route:
Now, let's go through the code and explain the implementation:
app/page.tsx, we create the main page component that renders the ImageUploadForm and displays the uploaded image URL when available.components/ImageUploadForm.tsx file contains the form component for handling file selection and upload. It uses the useState hook to manage the selected file and uploading state. When a file is selected and the form is submitted, it sends a POST request to our /api/upload endpoint with the file data.The app/api/upload/route.ts file contains the API route handler for uploading the image to Hostinger Cloud. It uses the NextRequest and NextResponse from Next.js 13's new App Router API 1. This handler does the following:
FormData object to send to the Hostinger APITo make this work, you'll need to set up a few things:
Install the required dependencies:
npm install form-data node-fetchAdd your Hostinger API key to your environment variables. Create a .env.local file in the root of your project and add:
HOSTINGER_API_KEY=your_hostinger_api_key_hereButton and Input) in the @/components/ui directory, or use a UI library of your choice.This implementation provides a complete solution for uploading images to Hostinger Cloud and immediately displaying the image URL to the user. The code handles file selection, uploading, error handling, and displaying the result.
Remember to handle any errors that may occur during the upload process and provide appropriate feedback to the user. Also, consider adding file type and size validations to ensure only appropriate images are uploaded.
To get a Hostinger Cloud File Storage API key to store data, you'll need to follow these steps:
I apologize for the confusion in my previous response. Hostinger doesn't currently offer a public API for their cloud storage service in the same way as some other cloud providers. However, I can guide you through a process to use Hostinger's file hosting capabilities within your web hosting account. Here's a general approach you can take:
app/api/upload/route.ts file:This code does the following:
Remember, this approach stores files on the same server as your application. For a production environment, you might want to consider using a dedicated file storage service or a content delivery network (CDN) for better performance and scalability.
Also, be sure to implement proper security measures, such as:
Lastly, always refer to Hostinger's official documentation and support channels for the most up-to-date information about their services and any potential API offerings they might introduce in the future.
FTP IP (hostname) ftp://217.21.82.234
FTP username u386199748.nesscoindustries.com
FTP port 21
Folder to upload files public_html
and password kafka
and bellow is
Active hostname
Directory
Username
ftp.nesscoindustries.com /home/u386199748/domains/nesscoindustries.com/public_html/businesscard u386199748.businesscard
now kindally make all thing working perfectly and write complete working code