如何修补 multer 文件名?

发布于 2025-01-20 14:36:28 字数 731 浏览 1 评论 0原文

const multerStorage = multer.diskStorage({
destination: (req, file, cb) =>{
    cb(null, '../client/public/uploadImg' )
},
filename :  (req, file, cb) => {
    const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
    cb(null, file.fieldname + '-' + uniqueSuffix + '_' + file.originalname)
  }

})

一旦我为文件制作了一个唯一的名称,并且我保存在uploadimg文件夹中,现在我需要捕获文件名,以便我可以在客户端上进行。

  {usrs.map((alldata, i)=>(
            <div key={i} >
                <img src={`/uploadImg/${alldata.photo}`} alt="..." />
                <h1> {alldata.name}</h1>
                <h3> {alldata.photo} </h3>
            </div>
        ))}
const multerStorage = multer.diskStorage({
destination: (req, file, cb) =>{
    cb(null, '../client/public/uploadImg' )
},
filename :  (req, file, cb) => {
    const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
    cb(null, file.fieldname + '-' + uniqueSuffix + '_' + file.originalname)
  }

})

Once I careated a unique name for my file and I saved in uploadImg folder now I need to catch the filename so I can fatch on my client side

  {usrs.map((alldata, i)=>(
            <div key={i} >
                <img src={`/uploadImg/${alldata.photo}`} alt="..." />
                <h1> {alldata.name}</h1>
                <h3> {alldata.photo} </h3>
            </div>
        ))}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌面超妹 2025-01-27 14:36:28

在某些时候,您的客户端正在发出 HTTP 请求(我假设是 Ajax 请求)来上传图像。

这将由一些服务器端代码处理。以下是文档中的示例:

const multer = require('multer')
const upload = multer({ dest: './public/data/uploads/' })
app.post('/stats', upload.single('uploaded_file'), function (req, res) {
   // req.file 是上面表格中的文件名,这里是 'uploaded_file'
   // req.body 将保存文本字段(如果有) 
   console.log(req.file, req.body)
});

您需要在对 Ajax 请求的响应中发送 req.file 的值(或者更确切地说是从它派生的 URL)。

由于您的客户端代码使用 JSX,因此我假设您使用的是 React.js。

客户端代码需要关注该响应并将结果存储在状态中。

然后,您可以在渲染组件时从状态读取值。

At some point, your client side is is making an HTTP request (which I assume is an Ajax request) to upload the image.

That will be handled by some server side code. Here is the example from the documentation:

const multer  = require('multer')
const upload = multer({ dest: './public/data/uploads/' })
app.post('/stats', upload.single('uploaded_file'), function (req, res) {
   // req.file is the name of your file in the form above, here 'uploaded_file'
   // req.body will hold the text fields, if there were any 
   console.log(req.file, req.body)
});

You need to send the value of req.file (or rather a URL derived from it) in the response to you make to the Ajax request.

Since your client side code is using JSX, I'm going to assume you are using React.js.

The client-side code needs to pay attention to that response and store the result in the state.

You can then read the value from the state when rendering the component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文