Javascript 将输入文件更改为本地文件的路径
我尝试使用一个脚本来提取图像中的坐标,在做了一点修改后,它对我来说很好。 但是我不知道如何从计算机上传pdf文件,服务器会告诉你文件在本地服务器上的位置 键入字符串php或var-javascript 但我无法理解在javascript中,在哪里可以更改它,以便不获取输入信息并使其成为我的默认值Javascript 将输入文件更改为本地文件的路径,javascript,html,jquery,pdf,Javascript,Html,Jquery,Pdf,我尝试使用一个脚本来提取图像中的坐标,在做了一点修改后,它对我来说很好。 但是我不知道如何从计算机上传pdf文件,服务器会告诉你文件在本地服务器上的位置 键入字符串php或var-javascript 但我无法理解在javascript中,在哪里可以更改它,以便不获取输入信息并使其成为我的默认值 <body> <button id="upload-button">Select PDF</button> <input type=&q
<body>
<button id="upload-button">Select PDF</button>
<input type="file" id="file-to-upload" accept="application/pdf" />
<div id="pdf-main-container">
<div id="pdf-loader">Preparando pdf...</div>
<div id="pdf-contents">
<div id="pdf-meta">
Elige la pagina donde es la firma</br>
<div id="pdf-buttons">
<button id="pdf-prev">Atras</button>
<button id="pdf-next">Siguiente</button>
</div>
<div id="page-count-container">Page <div id="pdf-current-page"></div> of <div id="pdf-total-pages"></div></div>
</div>
<canvas id="pdf-canvas" width="400"></canvas>
<div id="page-loader">Loading page ...</div>
<a id="download-image" href="#">Download PNG</a>
</div>
</div>
<script>
var __PDF_DOC,
__CURRENT_PAGE,
__TOTAL_PAGES,
__PAGE_RENDERING_IN_PROGRESS = 0,
__CANVAS = $('#pdf-canvas').get(0),
__CANVAS_CTX = __CANVAS.getContext('2d');
function showPDF(pdf_url) {
$("#pdf-loader").show();
PDFJS.getDocument({ url: pdf_url }).then(function(pdf_doc) {
__PDF_DOC = pdf_doc;
__TOTAL_PAGES = __PDF_DOC.numPages;
// Hide the pdf loader and show pdf container in HTML
$("#pdf-loader").hide();
$("#pdf-contents").show();
$("#pdf-total-pages").text(__TOTAL_PAGES);
// Show the first page
showPage(1);
}).catch(function(error) {
// If error re-show the upload button
$("#pdf-loader").hide();
$("#upload-button").show();
alert(error.message);
});;
}
function showPage(page_no) {
__PAGE_RENDERING_IN_PROGRESS = 1;
__CURRENT_PAGE = page_no;
// Disable Prev & Next buttons while page is being loaded
$("#pdf-next, #pdf-prev").attr('disabled', 'disabled');
// While page is being rendered hide the canvas and show a loading message
$("#pdf-canvas").hide();
$("#page-loader").show();
$("#download-image").hide();
// Update current page in HTML
$("#pdf-current-page").text(page_no);
// Fetch the page
__PDF_DOC.getPage(page_no).then(function(page) {
// As the canvas is of a fixed width we need to set the scale of the viewport accordingly
var scale_required = __CANVAS.width / page.getViewport(1).width;
// Get viewport of the page at required scale
var viewport = page.getViewport(scale_required);
// Set canvas height
__CANVAS.height = viewport.height;
var renderContext = {
canvasContext: __CANVAS_CTX,
viewport: viewport
};
// Render the page contents in the canvas
page.render(renderContext).then(function() {
__PAGE_RENDERING_IN_PROGRESS = 0;
// Re-enable Prev & Next buttons
$("#pdf-next, #pdf-prev").removeAttr('disabled');
// Show the canvas and hide the page loader
$("#pdf-canvas").show();
$("#page-loader").hide();
$("#download-image").show();
});
});
}
// Upon click this should should trigger click on the #file-to-upload file input element
// This is better than showing the not-good-looking file input element
$("#upload-button").on('click', function() {
$("#file-to-upload").trigger('click');
});
// When user chooses a PDF file
$("#file-to-upload").on('change', function() {
// Validate whether PDF
if(['application/pdf'].indexOf($("#file-to-upload").get(0).files[0].type) == -1) {
alert('Error : Not a PDF');
return;
}
$("#upload-button").hide();
// Send the object url of the pdf
showPDF(URL.createObjectURL($("#file-to-upload").get(0).files[0]));
});
// Previous page of the PDF
$("#pdf-prev").on('click', function() {
if(__CURRENT_PAGE != 1)
showPage(--__CURRENT_PAGE);
});
// Next page of the PDF
$("#pdf-next").on('click', function() {
if(__CURRENT_PAGE != __TOTAL_PAGES)
showPage(++__CURRENT_PAGE);
});
// Download button
$("#download-image").on('click', function() {
alert(__CANVAS.toDataURL()+"imagen");
$(this).attr('href', __CANVAS.toDataURL()).attr('download', 'page.png');
});
</script>
选择PDF
准备pdf。。。
伊丽格·拉帕吉娜·德拉菲尔马
阿特拉斯
西吉恩特
第页,共页
正在加载页面。。。
var\uuuuPDF\uDoc,
__当前页面,
__总页数,
__页面渲染正在进行=0,
__画布=$(“#pdf画布”).get(0),
__CANVAS_CTX=uu CANVAS.getContext('2d');
函数showPDF(pdf\U url){
$(“#pdf加载器”).show();
getDocument({url:pdf\u url})。然后(函数(pdf\u doc){
__PDF_DOC=PDF_DOC;
__总页数=\uuuuupdf\udoc.numPages;
//隐藏pdf加载程序并在HTML中显示pdf容器
$(“#pdf加载器”).hide();
$(“#pdf内容”).show();
$(“#pdf总页数”)。文本(#总页数);
//显示第一页
展示页(1);
}).catch(函数(错误){
//如果出现错误,请重新显示上载按钮
$(“#pdf加载器”).hide();
$(“#上载按钮”).show();
警报(错误消息);
});;
}
功能显示页(页码){
__页面渲染正在进行=1;
__当前页面=页码;
//在加载页面时禁用上一步和下一步按钮
$(“#pdf next,#pdf prev”).attr('disabled','disabled');
//在呈现页面时,隐藏画布并显示加载消息
$(“#pdf画布”).hide();
$(“#页面加载器”).show();
$(“#下载图像”).hide();
//用HTML更新当前页面
$(“#pdf当前页”)。文本(页码);
//取那一页
__PDF_DOC.getPage(页码)。然后(函数(页码){
//由于画布的宽度固定,我们需要相应地设置视口的比例
var scale_required=_CANVAS.width/page.getViewport(1).width;
//以所需比例获取页面的视口
var viewport=page.getViewport(需要缩放);
//设置画布高度
__CANVAS.height=viewport.height;
var renderContext={
canvasContext:\uuuu CANVAS\uCTX,
视口:视口
};
//在画布中呈现页面内容
render(renderContext).then(function(){
__页面渲染正在进行=0;
//重新启用上一步和下一步按钮
$(“#pdf next,#pdf prev”).removeAttr('disabled');
//显示画布并隐藏页面加载程序
$(“#pdf画布”).show();
$(“#页面加载器”).hide();
$(“#下载图像”).show();
});
});
}
//单击后,应触发单击#文件以上载文件输入元素
//这比显示外观不好的文件输入元素要好
$(“#上载按钮”)。在('单击',函数()上{
$(“#要上载的文件”).trigger('click');
});
//当用户选择PDF文件时
$(“#要上载的文件”)。在('change',function()上{
//验证是否为PDF
if(['application/pdf'].indexOf($(“#要上载的文件”).get(0).files[0].type)=-1){
警报(“错误:不是PDF”);
返回;
}
$(“#上载按钮”).hide();
//发送pdf的对象url
showPDF(URL.createObjectURL($(“#要上载的文件”).get(0.files[0]);
});
//PDF的上一页
$(“#pdf prev”)。on('单击',函数(){
如果(当前页面!=1)
显示页面(-\u当前页面);
});
//PDF的下一页
$(“#pdf next”)。在('click',function()上{
如果(当前页面!=\u总页面)
显示页面(+++u当前页面);
});
//下载按钮
$(“#下载图像”)。在('click',function()上{
警报(_CANVAS.toDataURL()+“imagen”);
$(this.attr('href',_ucanvas.toDataURL()).attr('download','page.png');
});