Next.js 如何访问上传到公用文件夹的文件? [英] How to access files uploaded to the public folder in Next.js?

查看:15
本文介绍了Next.js 如何访问上传到公用文件夹的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Next.js 中有一个项目.我有那个上传文件并在这个项目的公共 URL 中分享.

I have a project in Next.js. I have that upload files and share that in public URL to this project.

首先使用 npm run dev 我将文件上传到 public 文件夹,它工作正常,但是当我更改为 npm run start 并上传文件,文件上传到 public 文件夹但 URL http://mydomain/fileuploaded.jpg 它没有显示,很少见,但它在那里.

With npm run dev first I uploaded files to public folder and it worked fine, but when I change to npm run start and upload files, the files upload to public folder but with URL http://mydomain/fileuploaded.jpg it did not show, is rare but it's there.

我在互联网上搜索,但没有找到解决此问题的方法.

I searched on the Internet but I didn't find a solution for this problem.

推荐答案

来自 Next.js 文档:

只有在构建时位于公共目录中的资产才会由 Next.js 提供服务.在运行时添加的文件将不可用.

Only assets that are in the public directory at build time will be served by Next.js. Files added at runtime won't be available.

如果您想在运行时在应用程序中访问上传的文件,则必须将它们保存在其他地方.

You'll have to persist the uploaded files somewhere else if you want to have access to them in the app at run time.

或者,您可以在 Next.js 中设置自己的 自定义服务器,这将使您能够更好地控制服务静态文件/资产.

Alternatively, you could setup your own custom server in Next.js, which would give you more control to serve static files/assets.

您也可以改用 API 路由来实现类似的效果.请参阅 下一步.js 提供未包含在构建或源代码中的静态文件了解详细信息.

You can also achieve something similar using API routes instead. See Next.js serving static files that are not included in the build or source code for details.

这篇关于Next.js 如何访问上传到公用文件夹的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆