admin管理员组文章数量:1025522
I can't read of JSON file content, I tried to access the contents of the file by ReadFile but I did not success, I get string empty for content and url is undefined,
My Code:
<template>
<div class="large-12 medium-12 small-12 cell">
<input type="file" accept="application/JSON" @change="onFileChange" class="inputFile" />
</div>
</template>
<script>
import Vue from "vue";
export default {
data() {
return {};
},
methods: {
onFileChange(e) {
let files = e.target.files || e.dataTransfer.files;
if (!files.length) return;
this.readFile(files[0]);
},
readFile(file) {
let reader = new FileReader();
reader.onload = e => {
this.readContentFile = e.target.result;
console.log(this.readFile);
};
let url = reader.readAsDataURL(file);
let content =reader.result;
console.log(url);
console.log(content);
}
}
};
</script>
I can't read of JSON file content, I tried to access the contents of the file by ReadFile but I did not success, I get string empty for content and url is undefined,
My Code:
<template>
<div class="large-12 medium-12 small-12 cell">
<input type="file" accept="application/JSON" @change="onFileChange" class="inputFile" />
</div>
</template>
<script>
import Vue from "vue";
export default {
data() {
return {};
},
methods: {
onFileChange(e) {
let files = e.target.files || e.dataTransfer.files;
if (!files.length) return;
this.readFile(files[0]);
},
readFile(file) {
let reader = new FileReader();
reader.onload = e => {
this.readContentFile = e.target.result;
console.log(this.readFile);
};
let url = reader.readAsDataURL(file);
let content =reader.result;
console.log(url);
console.log(content);
}
}
};
</script>
Share
Improve this question
asked Mar 4, 2020 at 14:56
Nir AmirNir Amir
1303 silver badges14 bronze badges
1 Answer
Reset to default 5You almost had it. reader.onload is like a callback for when file has finished loading, so your console.logs near the bottom would happen before the file has loaded. Hopefully you should find this snippet helpful.
new Vue ({
el: "#app",
methods: {
onFileChange(e) {
let files = e.target.files || e.dataTransfer.files;
if (!files.length) return;
this.readFile(files[0]);
},
readFile(file) {
let reader = new FileReader();
reader.onload = e => {
console.log(e.target.result);
let json = JSON.parse(e.target.result);
};
reader.readAsText(file);
}
}
});
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.5.17/vue.js"></script>
<div class="large-12 medium-12 small-12 cell" id="app">
<input type="file" accept="application/json" @change="onFileChange">
</div>
I can't read of JSON file content, I tried to access the contents of the file by ReadFile but I did not success, I get string empty for content and url is undefined,
My Code:
<template>
<div class="large-12 medium-12 small-12 cell">
<input type="file" accept="application/JSON" @change="onFileChange" class="inputFile" />
</div>
</template>
<script>
import Vue from "vue";
export default {
data() {
return {};
},
methods: {
onFileChange(e) {
let files = e.target.files || e.dataTransfer.files;
if (!files.length) return;
this.readFile(files[0]);
},
readFile(file) {
let reader = new FileReader();
reader.onload = e => {
this.readContentFile = e.target.result;
console.log(this.readFile);
};
let url = reader.readAsDataURL(file);
let content =reader.result;
console.log(url);
console.log(content);
}
}
};
</script>
I can't read of JSON file content, I tried to access the contents of the file by ReadFile but I did not success, I get string empty for content and url is undefined,
My Code:
<template>
<div class="large-12 medium-12 small-12 cell">
<input type="file" accept="application/JSON" @change="onFileChange" class="inputFile" />
</div>
</template>
<script>
import Vue from "vue";
export default {
data() {
return {};
},
methods: {
onFileChange(e) {
let files = e.target.files || e.dataTransfer.files;
if (!files.length) return;
this.readFile(files[0]);
},
readFile(file) {
let reader = new FileReader();
reader.onload = e => {
this.readContentFile = e.target.result;
console.log(this.readFile);
};
let url = reader.readAsDataURL(file);
let content =reader.result;
console.log(url);
console.log(content);
}
}
};
</script>
Share
Improve this question
asked Mar 4, 2020 at 14:56
Nir AmirNir Amir
1303 silver badges14 bronze badges
1 Answer
Reset to default 5You almost had it. reader.onload is like a callback for when file has finished loading, so your console.logs near the bottom would happen before the file has loaded. Hopefully you should find this snippet helpful.
new Vue ({
el: "#app",
methods: {
onFileChange(e) {
let files = e.target.files || e.dataTransfer.files;
if (!files.length) return;
this.readFile(files[0]);
},
readFile(file) {
let reader = new FileReader();
reader.onload = e => {
console.log(e.target.result);
let json = JSON.parse(e.target.result);
};
reader.readAsText(file);
}
}
});
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.5.17/vue.js"></script>
<div class="large-12 medium-12 small-12 cell" id="app">
<input type="file" accept="application/json" @change="onFileChange">
</div>
本文标签: javascriptHow get data from JSON file by readfile in VueStack Overflow
版权声明:本文标题:javascript - How get data from JSON file by readfile in Vue? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745633421a2160299.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论