Skip to main content

Cloudinary linked

 Backend:

const express=require("express");
const app=express();
const cors=require("cors")
const dotenv=require('dotenv');
const cloudinary=require("cloudinary").v2;

dotenv.config();
app.use(cors())
cloudinary.config({
    cloud_name:process.env.CLOUDINARY_NAME,
    api_key:process.env.CLOUDINARY_API_KEY,
    api_secret:process.env.CLOUDINARY_API_SECRECT
})
app.use(express.json({limit:"50mb"}));
app.use(express.urlencoded({limit:"50mb",extended:true}))




app.post("/upload",async(req,res)=>{
    try {
        const fileStr=req.body.data;
        const uploadedResponse=await cloudinary.uploader.upload(fileStr,{upload_preset:"dev_setup"})
        console.log(uploadedResponse);
        res.json(uploadedResponse.url)
    } catch (error) {
        console.error(error);
    }
});


app.listen(3500,()=>{
    console.log("Server is running at port "+3500);
})



Frontend:


import './App.css';
import {useEffect, useState} from 'react'
import axios from 'axios';

function App() {
  const [file, setFile] = useState("");
  const [selectedFile, setselectedFile] = useState("");
  const [preview, setPreview] = useState("");
 
  const handleInput=(e)=>{
    const fileValue=e.target.files[0];
    previewFile(fileValue)
  }
  const previewFile=(image)=>{
    const reader=new FileReader();
    reader.readAsDataURL(image);
    reader.onloadend=()=>{
      setPreview(reader.result)
    }
  }

  const handleFormSubmit=(e)=>{
    e.preventDefault();
    if(!preview)return
    uploadImage(preview);
  }
  const uploadImage=async(base64EncodedImage)=>{
   
    try {
      await fetch("/upload",{
        method:"POST",
        headers:{"Content-type":"application/json"},
        body:JSON.stringify({data:base64EncodedImage})
      })
    } catch (error) {
      console.log(error);
    }
  }
  return (
   
    <div className="App">
      <form onSubmit={handleFormSubmit}>
        <input onChange={handleInput} type="file" name="image" value={file} id="" />
        <input type="submit" value="Upload" />
        </form>

      {preview &&(
        <img src={preview} alt=""  style={{"height":"300px"}}/>
      )}
    </div>
  );
}

export default App;


Comments

Post a Comment

Popular posts from this blog

IELTS Spoken Class Adminssion Scenario - 01

.......  Student: Hello, May I come in, sir ? Optional (student): May I sit ? Sir:  Please have a seat. Sir: How may I help you, Sir ? Student: I would like to admit in your spoken course. Sir: Oh sure. Student: How many days are there in a week ? Sir: There are three classes in a week. Student: What time do you offer class ? Sir: We have class at 11am / 4pm / 6pm / 8pm

Php Learning Time

 Differences of explode( ) and implode( ) in php: explode: একটা স্ট্রিংকে কোনো একটা সেপারেটরের বেসিসে অ্যারেতে কনভার্ট করে, যেমন  <?php $text="Hello How are you?"; print_r(explode(" ",$text)); ?> This will give output of  Array (     [0] => Hello     [1] => How     [2] => are     [3] => you? ) Differences of array_splice( ) and array_slice( ) ধরেন আসল অ্যারে হচ্ছে    $arr =[ "Hello" , "this" , "is" , "test" , "text" ];    এখন এটাকে স্লাইসিং করার জন্য আমরা উপরের দুইটা মেথড ই ব্যবহার করতে পারি , কিন্তু array_splice এটা ইউজ করলে অরজিনাল array ও চেঞ্জ হয়ে যাবে, মানে যদি আমরা এভাবে লিখি  $var2 = array_splice ( $arr , 0 , 2 );   তাইলে $var2 এর ভিতরে থাকবে ["Hello" , "this"] ,  আর অরজিনাল array তে বাকি থাকবে ["is", "test", "text"] কিন্তু যদি আমরা ইউজ করি তাহলে অরজিনাল array আগের মতোই থাকবে পাশাপাশি $var2 এর মধ্যে ভ্যালু গুলা এসে পড়বে  Array (     [0] => ...