BS架构客户端获取本机打印机.md
B/S架构客户端获取本机安装的打印机
使用a
标签访问本地执行程序查询当前电脑安装的打印机
实现方法请见浏览器快速打印,但该方法被调用的应用程序无法将获取到的打印机列表返回给浏览器。
我们可以做些调整,浏览器提交sessionID参数访问本地应用程序获得打印机列表,应用程序将打印机列表和SessionId发送给服务器,然后浏览器在访问服务器,通过sessionId获得本机打印机列表。
WebClentPrint 浏览器获取打印机的JavaScript核心代码为:
getPrinters: function() {
// 调用本地应用程序获得打印机列表,并将打印机列表和sessionId发送到服务器
setA('-getPrinters:https://webclientprint.azurewebsites.net/WebClientPrintAPI.ashx?sid=' + 'heopnxayuwrvq2pebrqxt4gj');
var delay_ms = (typeof wcppGetPrintersDelay_ms === 'undefined') ? 0 : wcppGetPrintersDelay_ms;
if (delay_ms > 0) {
// 根据sessionId从服务器获取打印机列表
setTimeout(function() {
$.get('https://webclientprint.azurewebsites.net/WebClientPrintAPI.ashx?getPrinters&sid=' + 'heopnxayuwrvq2pebrqxt4gj',function(data) {
if (data.length > 0) {
wcpGetPrintersOnSuccess(data)
} else {
wcpGetPrintersOnFailure()
}
});
},
delay_ms);
} else {
var fncGetPrinters = setInterval(getClientPrinters, wcppGetPrintersTimeoutStep_ms);
var wcpp_count = 0;
function getClientPrinters() {
if (wcpp_count <= wcppGetPrintersTimeout_ms) {
$.get('https://webclientprint.azurewebsites.net/WebClientPrintAPI.ashx?getPrinters&sid=' + 'heopnxayuwrvq2pebrqxt4gj', {'_': $.now()}, function(data) {
if (data.length > 0) {
clearInterval(fncGetPrinters);
wcpGetPrintersOnSuccess(data);
}
});
wcpp_count += wcppGetPrintersTimeoutStep_ms;
} else {
clearInterval(fncGetPrinters);
wcpGetPrintersOnFailure()
}
}
}
}