如何修补 multer 文件名?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在某些时候,您的客户端正在发出 HTTP 请求(我假设是 Ajax 请求)来上传图像。
这将由一些服务器端代码处理。以下是文档中的示例:
您需要在对 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:
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.