You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从网页调用本地软件打开指定本地文件?

从网页调用本地软件打开指定文件的解决方案

一、自定义URL协议+注册表配置(原生直接调用)

通过修改Windows注册表,给目标软件绑定带参数的URL协议,让浏览器能直接传递文件路径。

示例1:配置PotPlayer支持带文件路径的协议

  1. 新建文本文件,粘贴以下内容后保存为PotPlayer_Protocol.reg:
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\potplayer]
@="URL:PotPlayer Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\potplayer\shell]
@="open"

[HKEY_CLASSES_ROOT\potplayer\shell\open\command]
@="\"C:\\Program Files\\DAUM\\PotPlayer\\PotPlayerMini64.exe\" \"%1\""
  1. 右键文件选择「合并」,导入注册表。
  2. 网页中调用链接格式:
<a href="potplayer://open?file=D:\\Movies\\example.mp4">用PotPlayer打开视频</a>

注意:文件路径需做URL编码(如空格转%20),避免解析错误。

示例2:配置uTorrent支持种子文件调用

创建uTorrent_Protocol.reg文件并导入:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\utorrent]
@="URL:uTorrent Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\utorrent\shell]
@="open"

[HKEY_CLASSES_ROOT\utorrent\shell\open\command]
@="\"C:\\Program Files\\uTorrent\\uTorrent.exe\" \"%1\""

网页调用:

<a href="utorrent://open?file=D:\\Downloads\\example.torrent">用uTorrent打开种子</a>

二、本地HTTP服务器脚本(灵活扩展方案)

搭建轻量本地HTTP服务器,通过网页请求本地接口来调用软件,适合多软件、动态路径场景。

Python脚本示例(本地服务器)

新建local_file_opener.py:

from http.server import BaseHTTPRequestHandler, HTTPServer
import subprocess
import urllib.parse

class RequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        parsed_path = urllib.parse.urlparse(self.path)
        query_params = urllib.parse.parse_qs(parsed_path.query)
        
        if parsed_path.path == '/open':
            player = query_params.get('player', ['potplayer'])[0]
            file_path = query_params.get('file', [''])[0]
            
            if not file_path:
                self.send_error(400, 'Missing file parameter')
                return
            
            player_paths = {
                'potplayer': 'C:\\Program Files\\DAUM\\PotPlayer\\PotPlayerMini64.exe',
                'utorrent': 'C:\\Program Files\\uTorrent\\uTorrent.exe'
            }
            
            if player not in player_paths:
                self.send_error(400, 'Unsupported player')
                return
            
            try:
                decoded_path = urllib.parse.unquote(file_path)
                subprocess.Popen([player_paths[player], decoded_path])
                self.send_response(200)
                self.end_headers()
                self.wfile.write(b'File opened successfully')
            except Exception as e:
                self.send_error(500, str(e))
                return

if __name__ == '__main__':
    server_address = ('', 8000)
    httpd = HTTPServer(server_address, RequestHandler)
    print('Local file opener server running on port 8000...')
    httpd.serve_forever()

使用步骤

  1. 安装Python后运行该脚本。
  2. 网页调用链接格式:
<a href="http://localhost:8000/open?player=potplayer&file=D%3A%5CMovies%5Cexample.mp4">用PotPlayer打开视频</a>
  1. 设置开机自启:右键脚本创建快捷方式,将快捷方式放入C:\Users\[你的用户名]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup文件夹。

三、浏览器用户脚本(Tampermonkey辅助)

用Tampermonkey脚本拦截网页链接,直接触发本地程序调用,无需修改系统配置。

Tampermonkey脚本示例

// ==UserScript==
// @name         Local File Opener
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Open local files with specified software via browser
// @author       You
// @match        *://your-website.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    document.addEventListener('click', function(e) {
        const target = e.target.closest('a[data-open]');
        if (!target) return;
        
        e.preventDefault();
        const config = JSON.parse(target.getAttribute('data-open'));
        const { player, file } = config;
        
        let url;
        if (player === 'potplayer') {
            url = `potplayer://open?file=${encodeURIComponent(file)}`;
        } else if (player === 'utorrent') {
            url = `utorrent://open?file=${encodeURIComponent(file)}`;
        }
        
        if (url) {
            window.location.href = url;
        }
    });
})();

使用步骤

  1. 安装Tampermonkey扩展,新建脚本粘贴上述代码,修改@match为目标网站域名。
  2. 网页添加带自定义属性的链接:
<a href="#" data-open='{"player":"potplayer","file":"D:\\Movies\\example.mp4"}'>用PotPlayer打开视频</a>

内容的提问来源于stack exchange,提问作者Nafis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 02:10:38