Home >Backend Development >PHP Tutorial >JS怎么获取上传文件的内容

JS怎么获取上传文件的内容

PHPz
PHPzOriginal
2016-06-06 20:27:183494browse

JS怎么获取上传文件的内容

JS怎么获取上传文件的内容?

js 获取上传文件的字节数及内容

 

<div>
    上传文件 : <input type="file" name = "file" id = "fileId" /> 
    
    <button  type = "submit" name = "btn" value = "提交" id = "btnId" onclick="check()" /> 提交
</div>
<script>
    function check() {
        
        var objFile = document.getElementById("fileId");
        if(objFile.value == "") {
            alert("不能空")
        }
    
        console.log(objFile.files[0].size); // 文件字节数
        
        var files = $(&#39;#fileId&#39;).prop(&#39;files&#39;);//获取到文件列表
        if(files.length == 0){
            alert(&#39;请选择文件&#39;);
        }else{
            var reader = new FileReader();//新建一个FileReader
            reader.readAsText(files[0], "UTF-8");//读取文件 
            reader.onload = function(evt){ //读取完文件之后会回来这里
                var fileString = evt.target.result; // 读取文件内容
        }
    }
    
}

更多PHP相关知识,请访问PHP中文网!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn