最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get File object from Javascript - Stack Overflow

programmeradmin7浏览0评论

I have used this code for getting File object of a input file (I have tested it by console.log and it is correct).

var file = event.target.files[0];

The object code that I obtain it's like:

File
 lastModified: 1526981976000
​ lastModifiedDate: Date 2018-05-22T09:39:36.000Z
 Name:"path.png"
 size: 19776
 type: "image/png"
 webkitRelativePath: ""...

However, now I want to get that kind of File object, but through a image path saved into my file system. Is it possible?

I have used this code for getting File object of a input file (I have tested it by console.log and it is correct).

var file = event.target.files[0];

The object code that I obtain it's like:

File
 lastModified: 1526981976000
​ lastModifiedDate: Date 2018-05-22T09:39:36.000Z
 Name:"path.png"
 size: 19776
 type: "image/png"
 webkitRelativePath: ""...

However, now I want to get that kind of File object, but through a image path saved into my file system. Is it possible?

Share Improve this question asked May 22, 2018 at 13:45 DanielDaniel 491 gold badge1 silver badge4 bronze badges 3
  • 3 Possible duplicate of Local file access with javascript – user5224313 Commented May 22, 2018 at 13:47
  • 2 Not possible in-browser; the browser has no access to the file system. – Daniel Beck Commented May 22, 2018 at 13:47
  • Check File Object in Javascript – Sachin Bankar Commented May 22, 2018 at 13:50
Add a ment  | 

2 Answers 2

Reset to default 3
    function handleFiles() {
    "use strict";
    var inputElement = document.getElementById("input");
    var fileList = inputElement.files; /* now you can work with the file list */
    var file = {type:"", name:"", size:""};

    for(var k = 0; k < fileList.length; k++){   
    file.name.push(fileList[k].name);
    file.type.push(fileList[k].type);
    file.size.push(fileList[k].size);
    //and others
    }

This works for multiple images files try it

    <h2>Upload images ...</h2>
    <input type="file" id="input" multiple onchange="handleFiles(this.files)">

The browser can't access the file system of the client, you need to upload the image first

发布评论

评论列表(0)

  1. 暂无评论