【问题标题】:Display message in iframe, if there is a .doc file which cannot be displayed在 iframe 中显示消息,如果有无法显示的 .doc 文件
【发布时间】:2015-04-23 07:39:50
【问题描述】:

我正在使用 iframe 在页面上显示 pdf 或 doc 文件。 pdf 毫无疑问显示完美,但 doc 文件刚刚下载,我知道为什么。

现在的问题是我想在 iframe 中显示一条消息,说明该文件是 doc 文件,因此无法显示,需要下载才能查看。

到目前为止,我已经搜索并找到了在加载 iframe 时显示消息或任何内容的各种解决方案,但我想这里不是加载任何内容的情况。

这里是fiddle

【问题讨论】:

  • 发布一些代码,以便我们看到您尝试过的内容。小提琴也会有很大帮助

标签: javascript html iframe


【解决方案1】:

显示一些示例代码,以便我们为您提供最佳解决方案。你也可以使用 js fiddle。

否则,您可以添加 if 条件来检查检查文件类型并向用户显示警报通知。

【讨论】:

    【解决方案2】:

    在更改或设置 iframe 之前检查它是什么文件类型怎么样?

    如果是 pdf,则在 iframe 中显示文件,如果是 doc,则加载另一个带有消息的 html 文件。

    另外,正如其他人所说,请展示一些你所做的事情的例子!

    编辑:使用 javascript。看看这段代码,告诉我它是否有帮助 http://jsfiddle.net/7803gakc/1/

    // Not really sure about another way if it's going to be pure js/html and nothing server-side
    
    // Here's the location to the file
    var file = "http://www.ieee.org/documents/ieeecopyrightform.doc",
    
    // The frame being used
        frame = document.getElementById("frame"),
    
    // Files that will be allowed to be embedded based on extension
        extensions = ["pdf"],
    
    // This will tell us if it's right or not
        correct = false;
    
    // Loop through the extension list
    for(var i = 0; i < extensions.length; i++){
        // Check if the file url ends with the given extension
        if(file.substring(file.lastIndexOf(".")+1) == extensions[i]){
            // All conditions met, set to true!
            correct = true;
        }
    }
    
    if(correct){
        // Yay, it's correct!
        frame.src = file;
    }else{
        // It's wrong, show something else!
        frame.src = "http://example.com";
    }
    

    【讨论】:

    • 我事先没有文件信息,所以不可能。
    • 你需要提供一些代码,因为我真的不知道从这里去哪里。
    • 用新的小提琴更新了答案。希望能帮助到你! :)
    • 嘿感谢小提琴和更新的答案,但正如我所说,我没有提前获得文件信息。我正在使用 Liferay,它给我的文件路径为“[文件路径]/a4d0ab01-4624-45c3-96d4-9fa857e39029”。所以我这里没有文件扩展名。
    • 但是,根据我的搜索,通过使用文件 MIME 类型,我可以做到这一点......但这对于这个小任务来说还有很长的路要走,所以我问是否有其他选项或技巧.
    【解决方案3】:

    我找不到任何在 iframe 中显示消息的解决方案,可能是它没有提供,所以我切换到对象标签。

    <object data="[path to .doc]" type="application/msword">
    <p>It appears you do not have a Suitable plugin for this browser. You can download this file.
        <a href="[path to .doc]">
        <button type="button">download</button></a>
    </p>
    

    【讨论】:

    • 我在 chrome 上收到一条错误消息-不支持此插件...有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 2022-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多