Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 乱码问题 #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

feat: 乱码问题 #72

wants to merge 1 commit into from

Conversation

rong9490
Copy link

No description provided.

@imondo
Copy link

imondo commented Nov 14, 2023 via email

@rong9490
Copy link
Author

悬赏100元求解答

@JexLau
Copy link

JexLau commented Nov 15, 2023

protected async excel文件下载(url: string, payload: any) {
  const getBuffer = new Promise<Buffer>((resolve, reject) => {
    this.page.once('response', async (response) => {
      if (!response.url().includes(url)) return;
      try {
        const buffer = await response.buffer();
        console.log('拦截的buffer大小', buffer.length);
        resolve(buffer);
      } catch (error) {
        reject(error);
      }
    });

    this.page!.evaluate(
      (opts) => {
        return fetch(opts.url, {
          method: 'POST',
          body: JSON.stringify(opts.payload),
          headers: {
            'Content-Type': 'application/json;charset=UTF-8'
          },
        });
      },
      {
        url,
        payload,
      },
    );
  });

  const timer = new Promise<undefined>((resolve) => setTimeout(resolve, 1000 * 30));
  try {
    const fileBuffer = await Promise.race([getBuffer, timer]);
    if (fileBuffer) {
      console.log('内部fileBuffer', fileBuffer.length);
      const fs = require('fs');
      fs.writeFileSync('downloaded_file.xls', fileBuffer); // 直接将buffer写入文件
    } else {
      console.log('下载超时或未能正确接收文件');
    }
  } catch (error) {
    console.error('下载过程中出现错误', error);
  }
}

@SouthLink
Copy link

protected async excel文件下载(url: string, payload: any) {
  const getBuffer = new Promise<any>((resolve) => {
    this.page.once('response', async (response) => {
      if (!response.url().includes(url)) return;
      const text = await response.text(); // 使用text()方法获取文本数据
      const buffer = Buffer.from(text, 'binary'); // 手动将文本转换为Buffer
      console.log('拦截的buffer大小', buffer?.length);
      resolve(buffer);
    });

    this.page!.evaluate(
      (opts) => {
        return fetch(opts.url, {
          method: 'POST',
          body: JSON.stringify(opts.payload),
          headers: {
            responseType: 'arrayBuffer',
            'Content-Type': 'application/json;charset=UTF-8',
            'Content-Disposition':
              'attachment;filename=2023-11-01%E6%97%A5%E5%89%8D%E8%8A%82%E7%82%B9%E7%94%B5%E4%BB%B7%E6%9F%A5%E8%AF%A2.xls',
          },
        }).then(async (res) => {
          const text = await res.text();
          const blob = new Blob([text], { type: 'application/vnd.ms-excel;charset=UTF-8' });
          return blob;
        });
      },
      {
        url,
        payload,
      },
    );
  });
  const timer = new Promise<undefined>((resolve) => setTimeout(resolve, 1000 * 30));
  const fileBuffer: any = await Promise.race([getBuffer, timer]);

  console.log('内部fileBuffer', fileBuffer!.length);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants