【Next.js 入门教程系列】06-上传文件

原文链接

CSDN 的排版/样式可能有问题,去我的博客查看原文系列吧,觉得有用的话, 给我的点个star,关注一下吧

上一篇【Next.js 入门教程系列】05-数据库

上传文件

选择云平台

  • Amazon S3
  • Google Cloud
  • Microsoft Azure
  • Cloudinary

Cloudinary 安装与配置

前往Cloudinary注册账号,注册好之后使用 npm i next-cloudinary 安装。

在 .env 文件中添加以下内容

# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL="mysql://root:@localhost:3306/nextapp"
# 添加下面这行,并修改为你的 Cloud Name
NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME="<Your Cloud Name>"

上传文件

本章代码链接

Cloudinary 内置了几个上传组件供我们使用,在Cloudinary Widgets有详细的介绍

Cloudinary Upload Widgets

创建 /upload/page.tsx 并添加以下内容

"use client";
import React from "react";
import { CldUploadWidget } from "next-cloudinary";

const UploadPage = () => {
  return (
    // 这里的 preset 换成你自己的 preset
    <CldUploadWidget uploadPreset="">
      {({ open }) => (
        <button className="btn btn-secondary" onClick={() => open()}>
          Upload
        </button>
      )}
    </CldUploadWidget>
  );
};
export default UploadPage;

其中的 preset 需要我们在 Cloudinary 的 settings/upload 中添加,设置好后,复制名字到 uploadPreset="" 即可

Preset

最终显示效果如下

Upload

Cloudinary Library可以查看上传上去的文件

展示上传的文件

本章代码链接

可以调用 CldImage 组件来显示上传上去的文件。直接使用图片的 publicID 即可

"use client";
import React, { useState } from "react";
// import CldImage 用于展示图片
import { CldUploadWidget, CldImage } from "next-cloudinary";

interface CloudinrayResult {
  public_id: string;
}

const UploadPage = () => {
  const [publicId, setPublicId] = useState("");
  return (
    <>
      {publicId && (
        // 若图片Id不为空则渲染图片
        <CldImage src={publicId} width={270} height={180} alt={"a picture"} />
      )}
      <CldUploadWidget
        uploadPreset="bf9xjxfb"
        onUpload={(result, widget) => {
          if (result.event !== "success") return;
          //   成功上传后修改图片Id
          const info = result.info as CloudinrayResult;
          setPublicId(info.public_id);
        }}
      >
        {({ open }) => (
          <button className="btn btn-secondary" onClick={() => open()}>
            Upload
          </button>
        )}
      </CldUploadWidget>
    </>
  );
};
export default UploadPage;

自定义上传组件

本章代码链接

Cloudinary Demo可以自定义上传组件,包括颜色,文件来源,是否支持多文件等等

DIY component

你也可以直接在调用时添加参数:

<CldUploadWidget
  uploadPreset="bf9xjxfb"
  // 直接在这里设置
  options={{
    sources: ["local"],
  }}
  onUpload={(result, widget) => {
    if (result.event !== "success") return;
    const info = result.info as CloudinrayResult;
    setPublicId(info.public_id);
  }}
></CldUploadWidget>

CSDN 的排版/样式可能有问题,去我的博客查看原文系列吧,觉得有用的话, 给我的点个star,关注一下吧

下一篇讲身份验证

下一篇【Next.js 入门教程系列】07-身份验证

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值