admin管理员组文章数量:1325380
I intend to have a link in my webpage which will open a local text file and display its contents. This is what I have tried so far:
<a href='#' onclick='readTextFile("file:///F:/folder1/abc.txt")' title='Summary'><h3>Summary</h3></a>
function readTextFile(file)
{
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var allText = rawFile.responseText;
alert(allText);
}
}
}
rawFile.send(null);
}
This is the error I am getting:
XMLHttpRequest cannot load file:///F:/folder1/abc.txt. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
My webpage is running locally on a local server.
Is it even possible to open and read local files?Seems like something browsers should probably not allow.
I intend to have a link in my webpage which will open a local text file and display its contents. This is what I have tried so far:
<a href='#' onclick='readTextFile("file:///F:/folder1/abc.txt")' title='Summary'><h3>Summary</h3></a>
function readTextFile(file)
{
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var allText = rawFile.responseText;
alert(allText);
}
}
}
rawFile.send(null);
}
This is the error I am getting:
XMLHttpRequest cannot load file:///F:/folder1/abc.txt. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
My webpage is running locally on a local server.
Is it even possible to open and read local files?Seems like something browsers should probably not allow.
Share Improve this question asked Mar 7, 2017 at 12:28 karansabhanikaransabhani 1371 gold badge4 silver badges15 bronze badges 7- oooh, no ... just re-read the question ... you can't do that, oh god please let it never be doable in any browser! – Jaromanda X Commented Mar 7, 2017 at 12:31
- In most browsers, this is possible only for resources inside the folder where the page lives, or in a child folder. – Pekka Commented Mar 7, 2017 at 12:31
-
2
@Pekka웃 - he wants to open
file:///
fromhttp://
!! – Jaromanda X Commented Mar 7, 2017 at 12:32 - @Jaromanda ah, indeed. No, that's not possible at all. – Pekka Commented Mar 7, 2017 at 12:33
- 1 yep @karansabhani – blackmiaool Commented Mar 12, 2017 at 15:41
4 Answers
Reset to default 3For HTML5 pliant websites you can use the new APIs available with HTML5.
HTML5 FileReader interface can be used to asynchronously read a file through familiar JavaScript event handling. It provides the following functions:
- readAsText
- readAsBinaryString
- readAsDataURL
- readAsArrayBuffer
Please follow this treehouse blog (contains demo too) and also this for your reference.
new answer:
It is still possible.
- Use chrome
- Install tampermonkey extension
- Check the checkbox to make it can access local files.
- Add a script into it as below:
// ==UserScript==
// @name read localfile
// @namespace http://tampermonkey/
// @version 0.1
// @description read localfile
// @author blackmiaool
// @match http://stackoverflow./*
// @match https://stackoverflow./*
// @grant GM_getResourceText
// @resource b file://C:\Users\blackmiaool\the-path-of-the-file
// ==/UserScript==
(function() {
'use strict';
var a=GM_getResourceText("b");
console.log("The file's content is ",a);
})();
Remember to correct the file path, and test it on this page.
old answer:
Sure it's possible. If your index.html file locates at "/some-path/index.html", just put your abc.txt at "/some-path/abc.txt". And change the "file:///F:/folder1/abc.txt" to "./abc.txt".
Simply "NO, You can't read any files from the server deployed locally on your machine." In order to access any resource, one must have the resource available on your sever. Where your current page is being served from.
The file need to be in your web application to link to it. Allowing javascript to access your machine outside the application is a security risk and is not allowed. A back end process can read the file then give you the results but you can not read directly from javascript to a file on your machine
本文标签: javascriptHow do I open a local text file and display the contents in my browserStack Overflow
版权声明:本文标题:javascript - How do I open a local text file and display the contents in my browser? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742191911a2430370.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论