weixin_42504707 2024-08-23 16:35 采纳率: 0%
浏览 8

react 新窗口pdf显示标题

上传pdf在新窗口打开时,显示的标题是一串uuid。上传的pdf属性里没有title。能改成文件名显示吗?

img

img

img

代码:

img

  • 写回答

1条回答 默认 最新

  • 吃不了席 2024-08-23 17:05
    关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    可以在新窗口打开PDF时,通过设置window.opentitle参数来修改标题。以下是一个示例代码:

    import React from 'react';
    import { useRef } from 'react';
    
    const App = () => {
      const ref = useRef(null);
    
      const openPdfInNewWindow = (url) => {
        const newWindow = window.open(url, '_blank', 'title=文件名');
        if (newWindow) {
          newWindow.document.title = '文件名';
        }
      };
    
      return (
        <div>
          <input type="file" ref={ref} onChange={(e) => {
            const file = e.target.files[0];
            const url = URL.createObjectURL(file);
            openPdfInNewWindow(url);
          }} />
        </div>
      );
    };
    
    export default App;
    

    这段代码中,我们首先使用useRef创建了一个ref,然后在input元素的onChange事件中获取选中的文件,并将其转换为一个URL。接着,我们调用openPdfInNewWindow函数,将URL作为参数传递。在这个函数中,我们使用window.open打开一个新的窗口,并设置title参数为文件名。最后,我们将新窗口的文档标题设置为文件名

    评论

报告相同问题?

问题事件

  • 创建了问题 8月23日