如何在本地选择文件

时间:2018-08-19 04:47:49

标签: javascript html csv

我收到了此代码,但我不明白如何选择代码中的文件。我需要跳过选择菜单。我需要选择的文件始终与此文件位于同一文件夹中。该代码用于获取两个CSV文件列,并放入数组中,然后旋转框架。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body style="margin:0; padding:0">
  <div id="hidden_div">
    <form action="javascript:void(0);" id="the_form">
      <input type="file" id="the_file" required="required" accept=".csv"/>
      <input type="submit" value="Rotate!"/>
    </form>
    <div id="file_info"></div>
    <div id="list"></div>
  </div>
<script type="text/javascript">
var arr=[];
var urls=[];
var times=[];
var idx=0;
var currentFrame = "f"
var nextFrame = "g"
var firstEverRotation = true

 function fileInfo(e){             // CSV file selector
   var file = e.target.files[0];
     return;
  }
 function handleFileSelect(){   // Function for CSV file parsing and filling arrays
  var file = document.getElementById("the_file").files[0];
  var reader = new FileReader();
  reader.onload = function(file) {  // Read the CSV file and split it into arrays
              var content = file.target.result; // Results from the FileReader
              var rows = file.target.result.split(/[\r\n|\n]+/);  // Split the contents of the file into rows based on carriage returns
              for (var i = 1; i < rows.length; i++){  // For each row, split the contents into columns separated by commas (i starts at 1 to skip column headers in CSV)
                arr = rows[i].split(',');
                for (var j = 0; j < arr.length; j++){ // Split the URL's and times into separate arrays
                 urls.push(arr[j]);
                 j++;
                 times.push(arr[j]);
                }
              }
          showUrl();
          };
  reader.readAsText(file);
  };



</script>
<br><br>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

你裹尸布试试

Run as administrator
/*
filedrag.js - HTML5 File Drag & Drop demonstration
*/
(function() {

	// getElementById
	function $id(id) {
		return document.getElementById(id);
	}


	// output information
	function Output(msg) {
		var m = $id("messages");
		m.innerHTML = msg + m.innerHTML;
	}


	// file drag hover
	function FileDragHover(e) {
		e.stopPropagation();
		e.preventDefault();
		e.target.className = (e.type == "dragover" ? "hover" : "");
	}


	// file selection
	function FileSelectHandler(e) {

		// cancel event and hover styling
		FileDragHover(e);

		// fetch FileList object
		var files = e.target.files || e.dataTransfer.files;

		// process all File objects
		for (var i = 0, f; f = files[i]; i++) {
			ParseFile(f);
		}

	}


	// output file information
	function ParseFile(file) {

		Output(
			"<p>File information: <strong>" + file.name +
			"</strong> type: <strong>" + file.type +
			"</strong> size: <strong>" + file.size +
			"</strong> bytes</p>"
		);

	}


	// initialize
	function Init() {

		var fileselect = $id("fileselect"),
			filedrag = $id("filedrag"),
			submitbutton = $id("submitbutton");

		// file select
		fileselect.addEventListener("change", FileSelectHandler, false);

		// is XHR2 available?
		var xhr = new XMLHttpRequest();
		if (xhr.upload) {

			// file drop
			filedrag.addEventListener("dragover", FileDragHover, false);
			filedrag.addEventListener("dragleave", FileDragHover, false);
			filedrag.addEventListener("drop", FileSelectHandler, false);
			filedrag.style.display = "block";

			// remove submit button
			submitbutton.style.display = "none";
		}

	}

	// call initialization file
	if (window.File && window.FileList && window.FileReader) {
		Init();
	}


})();
/*
Styles for HTML5 File Drag & Drop demonstration
*/
body
{
	font-family: "Segoe UI", Tahoma, Helvetica, freesans, sans-serif;
	font-size: 90%;
	margin: 10px;
	color: #333;
	background-color: #fff;
}

h1, h2
{
	font-size: 1.5em;
	font-weight: normal;
}

h2
{
	font-size: 1.3em;
}

legend
{
	font-weight: bold;
	color: #333;
}

#filedrag
{
	display: none;
	font-weight: bold;
	text-align: center;
	padding: 1em 0;
	margin: 1em 0;
	color: #555;
	border: 2px dashed #555;
	border-radius: 7px;
	cursor: default;
}

#filedrag.hover
{
	color: #f00;
	border-color: #f00;
	border-style: solid;
	box-shadow: inset 0 3px 4px #888;
}

img
{
	max-width: 100%;
}

pre
{
	width: 95%;
	height: 8em;
	font-family: monospace;
	font-size: 0.9em;
	padding: 1px 2px;
	margin: 0 0 1em auto;
	border: 1px inset #666;
	background-color: #eee;
	overflow: auto;
}

#messages
{
	padding: 0 10px;
	margin: 1em 0;
	border: 1px solid #999;
}

#progress p
{
	display: block;
	width: 240px;
	padding: 2px 5px;
	margin: 2px 0;
	border: 1px inset #446;
	border-radius: 5px;
	background: #eee url("progress.png") 100% 0 repeat-y;
}

#progress p.success
{
	background: #0c0 none 0 0 no-repeat;
}

#progress p.failed
{
	background: #c00 none 0 0 no-repeat;
}