javascript - AdminBro 中文件上传的自定义提供程序问题
问题描述
我使用 imagekit 和 AdminBro 为文件上传创建自定义提供程序。这里是:
const { BaseProvider } = require("@admin-bro/upload");
const ImageKit = require("imagekit");
const imagekit = new ImageKit({
publicKey: process.env.IMAGE_KIT_PUBLIC_KEY,
privateKey: process.env.IMAGE_KIT_PRIVATE_KEY,
urlEndpoint: process.env.IMAGE_KIT_URL,
});
class UploadProvider extends BaseProvider {
constructor() {
super(process.env.IMAGE_KIT_URL);
}
async upload(file, key) {
console.log(file);
console.log(key);
const imageBinary = Buffer.from(JSON.stringify(file));
imagekit.upload(
{
file: imageBinary,
fileName: "test.jpg",
},
function (error, result) {
if (error) console.log(error);
else console.log(result);
}
);
return true;
}
async delete(key) {
imagekit.deleteFile(key, function (error, result) {
if (error) console.log(error);
else {
console.log(result);
return true;
}
});
}
async path(key, bucket) {
return bucket + key;
}
}
module.exports = UploadProvider;
当我上传一个 img 文件时,我得到以下响应:
{
fileId: '6065faae524afb4c982ea36b',
name: 'test_lQ-cxEt5i0.jpg',
size: 991,
filePath: '/test_lQ-cxEt5i0.jpg',
url: 'https://ik.imagekit.io/my-site/test_lQ-cxEt5i0.jpg',
fileType: 'non-image'
}
和 Imagekit 仪表板: jpg 扩展名丢失
所以文件被发送到 imagekit 但它丢失了扩展名。
这也是console.log(文件)中的内容:
{
size: 45742,
path: 'C:\\Users\\adibu\\AppData\\Local\\Temp\\upload_7627ceeabb5e466ad9e4c780613105f7',
name: 'candle.jpg',
type: 'image/jpeg',
hash: null,
_events: {},
_eventsCount: 0,
_writeStream: {
fd: null,
pos: undefined,
path: 'C:\\Users\\adibu\\AppData\\Local\\Temp\\upload_7627ceeabb5e466ad9e4c780613105f7',
mode: 438,
flags: 'w',
start: undefined,
closed: true,
_events: {},
bytesWritten: 45742,
_eventsCount: 0,
_maxListeners: undefined,
_writableState: {
sync: false,
ended: true,
ending: true,
length: 0,
corked: 0,
closed: true,
writing: false,
onwrite: [Function: bound onwrite],
writecb: null,
allNoop: true,
errored: null,
finished: true,
writelen: 0,
buffered: [],
needDrain: true,
destroyed: true,
pendingcb: 0,
emitClose: true,
objectMode: false,
allBuffers: true,
finalCalled: false,
constructed: true,
prefinished: true,
autoDestroy: true,
errorEmitted: false,
closeEmitted: true,
highWaterMark: 16384,
decodeStrings: true,
bufferedIndex: 0,
defaultEncoding: 'utf8',
bufferProcessing: false,
afterWriteTickInfo: null
}
},
_maxListeners: undefined,
lastModifiedDate: 2021-04-01T16:54:05.017Z
}
我不知道为什么文件在上传时丢失了扩展名,有人知道解决方案吗?
解决方案
推荐阅读
- java - 使用java登录页面首先显示错误并且没有HTML
- amazon-web-services - Terraform 数据 aws_lambda_function.existing 抛出错误:获取 Lambda 函数(MyLambda)时出错:ResourceNotFoundException:找不到函数
- c - 从一个 fork 进程中获得 3 个 Pid?
- python - 针对此错误在 GOOGLE Colab 中安装软件包的最有效方法是什么?
- sql - Database.Log not logging appended SQL from interceptor in Entity Framework
- php - 如何双重加入sql查询?
- python - PyOpengl:更改 gluPerspective 变量会使立方体闪烁
- php - facebook/php-webdriver flickFromElement W3C 错误
- angular - 必须做什么才能在离开组件时销毁 ngAfterViewInit 初始化?
- python - 将货币格式的字符串转换为保持美分、点分隔数据框的浮点数