如何使用JavaScript获取具有100个字符的base64图像文件,reactjs

发布于 2025-02-08 10:21:27 字数 1822 浏览 1 评论 0原文

我正在尝试在上传文件列表后获取Base64(fileReader)图像数据 但是不能检索其名称超过100个字符的文件。 有没有办法解决此问题,我不想更改文件名,因为它的数量太多了。您可以看到随附的照片。

文件名:

  1. 凯尔·拉尔森(Kyle Larson)77 NASCAR赛车运动经典T恤,男士T恤,连帽衫-KatyCollection -katyCollection 1.jpg => Work
  2. 凯尔·拉尔森(Kyle Larson)钻探NASCAR赛车运动经典T恤,男士T恤,连帽衫-KatyCollection 1.jpg =>不工作

这是我的代码

function getBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = error => reject(error);
 });
}
async onBeforeUpload(file) {
    this.setState({ loadingUpload: true })
    var path = file.webkitRelativePath;
    let pathArray = path.split('/');
    let fName;
    if (pathArray.length == 2) {
        fName = file.name.replace(/\.[^/.]+$/, "");
    } else {
        fName = pathArray.slice(-2)[0];
    }
    //console.log(file.name.length,": ", file.name)
    let data = await getBase64(file);
    const objectImg = {
        folderName: fName,
        description: "<p>" + fName + "</p>\n",
        images: {
            data: data,
        }
     }
     this.setState({ imageList: [...this.state.imageList, objectImg] })
  }

更新:

我使用Ant Design的上传组件和上传目录

<Upload
    beforeUpload={(file) => this.onBeforeUpload(file)}
    directory
    showUploadList={false}
    accept="image/*"
    customRequest={() => {
        return false;
    }}
>
    <Button className="table-button" type="primary">Upload</Button>
</Upload>

预先提前

I'm trying to get the base64 (FileReader) image data after the file list is uploaded
But files whose names are longer than 100 characters cannot be retrieved.
Is there a way for me to solve this problem, I don't want to change the filename because the number of it is too much. You can see the attached photo.

enter image description here

File name:

  1. Kyle Larson 77 Nascar Racing Sports Classic Tee, Men's T-Shirt, Hoodie - Katycollection 1.jpg => work
  2. Kyle Larson Drilling for Oil Nascar Racing Sports Classic Tee, Men's T-Shirt, Hoodie - Katycollection 1.jpg => Not working

Here is my code

function getBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = error => reject(error);
 });
}
async onBeforeUpload(file) {
    this.setState({ loadingUpload: true })
    var path = file.webkitRelativePath;
    let pathArray = path.split('/');
    let fName;
    if (pathArray.length == 2) {
        fName = file.name.replace(/\.[^/.]+$/, "");
    } else {
        fName = pathArray.slice(-2)[0];
    }
    //console.log(file.name.length,": ", file.name)
    let data = await getBase64(file);
    const objectImg = {
        folderName: fName,
        description: "<p>" + fName + "</p>\n",
        images: {
            data: data,
        }
     }
     this.setState({ imageList: [...this.state.imageList, objectImg] })
  }

Update:

I use ant design's Upload component and upload directory

<Upload
    beforeUpload={(file) => this.onBeforeUpload(file)}
    directory
    showUploadList={false}
    accept="image/*"
    customRequest={() => {
        return false;
    }}
>
    <Button className="table-button" type="primary">Upload</Button>
</Upload>

Thanks in advance

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文