[英]React.js, attribute in object is undefined but exist in object with non-null value
I want to add picture files with Dropzone and show preview on browser.我想用 Dropzone 添加图片文件并在浏览器上显示预览。 So, I have to get
src
which can use at <img />
.所以,我必须获得可以在
<img />
处使用的src
。 So I do this process:所以我做这个过程:
const handleAddImages = async (inputImages: any[]): Promise<void> => {
const imagesWithSrc = inputImages.map((img) => {
const reader = new FileReader();
new Promise((resolve, reject) => {
reader.onloadend = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(img);
}).then(() => (img.src = reader.result));
return img;
});
const nextImages = [...currentImages, ...imagesWithSrc];
handleOnChange({
//set state
target: {
name: "images",
value: nextImages,
},
});
};
And show the images in this component:并在此组件中显示图像:
export const FileList = ({
images
}: any) => {
const imgs: any[] = images;
return (
<Wrapper>
{imgs.map((img, index) => (
<ImgBox key={index}>
<img src={img.src} />
</ImgBox>
))}
</Wrapper>
);
};
I have an images array imgs
and contains image file objects.我有一个图像数组
imgs
并包含图像文件对象。 When I do this:当我这样做时:
console.log(imgs[0]);
it will output every attributes in image file object of index 0 including the image source src
in base64 from filereader API.它将输出索引 0 的图像文件对象中的每个属性,包括来自文件阅读器 API 的 base64 中的图像源
src
。
but when I do this:但是当我这样做时:
console.log(imgs[0].src);
it will output undefined
,它将输出
undefined
,
even I do console.log in map like this:即使我像这样在地图中执行 console.log :
imgs.map((img, index) => {
console.log(img);
console.log(img.src);
})
and get the same result, what is the problem?并得到相同的结果,有什么问题?
console.log("imgs[0]", imgs[0]);
console.log("imgs[0].src", imgs[0].src);
ouput:输出:
By the way, I can get the size attribute by imgs[0].size
at first, and get the imgs[0].src
after add another picture.顺便说一句,我可以先通过
imgs[0].size
获取size属性, imgs[0].size
添加一张图片后获取imgs[0].src
。
As defined in the W3C File API , the File interface (which extends Blob ) does NOT have an src
property.正如W3C File API 中所定义的, File接口(扩展Blob )没有
src
属性。 While your environment may display an src
property, it appears to be inaccessable from your code, and may be added by your browser's runtime or another part of your code in a way that makes it inaccessable.虽然您的环境可能会显示一个
src
属性,但它似乎无法从您的代码中访问,并且可能由您的浏览器的运行时或您的代码的其他部分以使其无法访问的方式添加。 Honestly, without more information, I cannot say for sure where the src
property is coming from.老实说,没有更多信息,我无法确定
src
属性来自哪里。
If you want to base64-ify your File object, try using the FileReader API:如果要对 File 对象进行 base64 化,请尝试使用FileReader API:
let blob = imgs[0];
let reader = new FileReader();
reader.readAsDataURL(blob);
reader.onload = function() {
console.log(reader.result); //-> "data:image/png;base64,iVBORw..."
};
It seems to be problem with filereader API and I solved my problem with this answer.文件阅读器 API 似乎有问题,我用这个答案解决了我的问题。 https://stackoverflow.com/a/34495914/16426251
https://stackoverflow.com/a/34495914/16426251
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.