iT邦幫忙

0

把選擇excel檔案改變成固定檔案

請教各位大神,以下是我的把excel的內容輸出到html的function的部分,最下面是input跟button,目前的方式是利用input的方式選擇excel檔案,然後button送出後顯示檔案內容,想問有沒有辦法把"選擇檔案"的方式,改變成路徑,讓一開啟網頁就已經完成原本的"選擇檔案再送出"這樣的動作?

function myController($scope){
			$scope.uploadExcel = function(){
				var myFile = document.getElementById('file');
				var input = myFile;
				var reader = new FileReader();
				reader.onload = function(){
					var fileData = reader.result;
					var workbook = XLSX.read(fileData, {type: 'binary'});
					workbook.SheetNames.forEach(function(sheetName){
						var rowObject = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
						excelJsonObj = rowObject;
					});
					for(var  i = 0; i < excelJsonObj.length; i++){
						var data = excelJsonObj[i];
						$('#mySelect').append("<option value='"+data.Column1+"'>"+data.Column1.substring(19));
					}		
				};		
				reader.readAsBinaryString(input.files[0]);
			};
		}
        
        
<form enctype="multipart/form-data">
				<input type="file" id="file">
				<button type="submit" value='submit' ng-click="uploadExcel()"> Reflash </button>
			</form>
看更多先前的討論...收起先前的討論...
dragonH iT邦超人 5 級 ‧ 2019-05-15 15:21:09 檢舉
那你的檔案路徑哪裡來?
目前是input去選擇檔案,假設是C:\Users\ting\Desktop\test.xlsx這個檔案,選擇後按button後顯示內容,
想要改成一開啟網頁就已經顯示test.xlsx的內容的方法
<!DOCTYPE html>
<html ng-app='myApp'>
<head>
<meta charset="utf-8">
<title>Reading Excel</title>
<meta name="viewport" content="width=device-width, initial-scale=10">

<script type="text/javascript" src="bs/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="bs/angular.min.js"></script>
<script type="text/javascript" src="bs/xlsx.full.min.js"></script>
<script type="text/javascript">
//get Subject & Preview in excel
var app = angular.module('myApp', []);
app.controller('MyController', ['$scope', myController]);
var excelJsonObj = [];

function myController($scope){
$scope.uploadExcel = function(){
var myFile = document.getElementById('file');
var input = myFile;
var reader = new FileReader();
reader.onload = function(){
var fileData = reader.result;
var workbook = XLSX.read(fileData, {type: 'binary'});
workbook.SheetNames.forEach(function(sheetName){
var rowObject = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
excelJsonObj = rowObject;
});
for(var i = 0; i < excelJsonObj.length; i++){
var data = excelJsonObj[i];
$('#mySelect').append("<option value='"+data.Column1+"'>"+data.Column1.substring(19));
}
};
reader.readAsBinaryString(input.files[0]);
};
}
function ChangeText(s)
{
for (var j = 0; j < excelJsonObj.length; j++){
var data2 = excelJsonObj[j];
if( data2.Column1 == s){
document.getElementById("p1").innerHTML=data2.Column2;
}
}
}
</script>
</head>
<p>[Certificated Patch]</p>
<body ng-controller='MyController'>
<div>
<form enctype="multipart/form-data">
<input type="file" id="file">
<button type="submit" value='submit' ng-click="uploadExcel()"> Reflash </button>
</form>
</div>

<select id="mySelect" onclick="ChangeText(this.options[this.options.selectedIndex].value)">
</select>
<hr>
<span id="p1">Select which one you want!</p>
</body>
</html>

這是目前的
froce iT邦大師 1 級 ‧ 2019-05-15 15:45:41 檢舉
應該是不行,違反瀏覽器的安全性。
dragonH iT邦超人 5 級 ‧ 2019-05-15 16:05:02 檢舉
我記得不行唷

https://stackoverflow.com/questions/1696877/how-to-set-a-value-to-a-file-input-in-html

https://stackoverflow.com/questions/23279227/set-file-path-for-input-type-file

你連真正的路徑好像都拿不到

只會拿到

C:\fakepath\test.xlsx
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
㊣浩瀚星空㊣
iT邦大神 1 級 ‧ 2019-05-15 16:50:04
最佳解答

本機安全性原則,網頁程式是無法直接去請求本機端檔案的。
所以選擇檔案是必要性的東西。

其實這也是因該的,因為如果網頁程式可以隨便跟你的本機要目錄或檔案名稱的話。
那就太可怕了。

了解 感謝各位大神的解惑

我要發表回答

立即登入回答