浏览器测试: Chrome版本:52.0.2743.116
这是一个简单的JavaScript,旨在从本地打开像'C:\002.jpg'这样的图像文件。
function run(){
var URL = "file:///C:\002.jpg";
window.open(URL, null);
}
run();
这是我的示例代码。 https://fiddle.jshell.net/q326vLya/3/
请给我任何适当的建议。
浏览器测试: Chrome版本:52.0.2743.116
这是一个简单的JavaScript,旨在从本地打开像'C:\002.jpg'这样的图像文件。
function run(){
var URL = "file:///C:\002.jpg";
window.open(URL, null);
}
run();
这是我的示例代码。 https://fiddle.jshell.net/q326vLya/3/
请给我任何适当的建议。
我们在课堂上经常使用Chrome浏览器,它是与本地文件一起工作的必备工具。
我们一直在使用的是“Web Server for Chrome”。您可以启动它,选择要使用的文件夹,然后转到URL(如您选择的127.0.0.1:端口)。
这是一个简单的服务器,不能使用PHP,但对于简单的工作,可能是您的解决方案:
https://chrome.google.com/webstore/detail/web-server-for-chrome/ofhbbkphhbklhfoeikjpcbhemlocgigb
1) 打开终端并输入
npm install -g http-server
2) 进入您要提供文件的根目录,然后输入:
http-server ./
3) 阅读终端输出,类似 http://localhost:8080
的东西将会出现。
其中所有内容都允许被获取。例如:
background: url('http://localhost:8080/waw.png')
;
好的,大家好,我完全理解这个错误信息背后的安全原因,但有时候我们确实需要一个变通方法...这就是我的方法。它使用ASP.Net(而不是这个问题基于的JavaScript),但希望对某些人有用。
我们公司内部的应用程序有一个网页,用户可以在其中创建一个快捷方式列表,以便访问网络中分散的有用文件。当他们点击其中一个快捷方式时,我们希望能够打开这些文件...但当然,Chrome的错误会阻止这一点。
这个网页使用AngularJS 1.x列出各种快捷方式。<a href..>
元素,但是当用户点击其中一个链接时,这会产生“不允许加载本地资源”的错误。<div ng-repeat='sc in listOfShortcuts' id="{{sc.ShtCut_ID}}" class="cssOneShortcutRecord" >
<div class="cssShortcutIcon">
<img ng-src="{{ GetIconName(sc.ShtCut_PathFilename); }}">
</div>
<div class="cssShortcutName">
<a ng-href="{{ sc.ShtCut_PathFilename }}" ng-attr-title="{{sc.ShtCut_Tooltip}}" target="_blank" >{{ sc.ShtCut_Name }}</a>
</div>
</div>
<a href..>
元素,调用我 Angular 控制器中的一个函数...<div ng-click="OpenAnExternalFile(sc.ShtCut_PathFilename);" >
{{ sc.ShtCut_Name }}
</div>
这个函数本身非常简单...
$scope.OpenAnExternalFile = function (filename) {
//
// Open an external file (i.e. a file which ISN'T in our IIS folder)
// To do this, we get an ASP.Net Handler to manually load the file,
// then return it's contents in a Response.
//
var URL = '/Handlers/DownloadExternalFile.ashx?filename=' + encodeURIComponent(filename);
window.open(URL);
}
DownloadExternalFile.aspx
的处理程序文件,其中包含以下代码:namespace MikesProject.Handlers
{
/// <summary>
/// Summary description for DownloadExternalFile
/// </summary>
public class DownloadExternalFile : IHttpHandler
{
// We can't directly open a network file using Javascript, eg
// window.open("\\SomeNetworkPath\ExcelFile\MikesExcelFile.xls");
//
// Instead, we need to get Javascript to call this groovy helper class which loads such a file, then sends it to the stream.
// window.open("/Handlers/DownloadExternalFile.ashx?filename=//SomeNetworkPath/ExcelFile/MikesExcelFile.xls");
//
public void ProcessRequest(HttpContext context)
{
string pathAndFilename = context.Request["filename"]; // eg "\\SomeNetworkPath\ExcelFile\MikesExcelFile.xls"
string filename = System.IO.Path.GetFileName(pathAndFilename); // eg "MikesExcelFile.xls"
context.Response.ClearContent();
WebClient webClient = new WebClient();
using (Stream stream = webClient.OpenRead(pathAndFilename))
{
// Process image...
byte[] data1 = new byte[stream.Length];
stream.Read(data1, 0, data1.Length);
context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", filename));
context.Response.BinaryWrite(data1);
context.Response.Flush();
context.Response.SuppressContent = true;
context.ApplicationInstance.CompleteRequest();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
就是这样。
现在,当用户点击我的快捷方式链接之一时,它调用OpenAnExternalFile
函数,该函数打开此.ashx文件,并将要打开的文件的路径+文件名传递给它。
此处理程序代码加载文件,然后在HTTP响应中返回其内容。
工作完成,网页打开外部文件。
呼!再说一遍 - Chrome抛出“Not allowed to load local resources
”异常有原因,所以请小心...但我发布这个代码只是为了证明这是一个相当简单的方法来解决这个限制。
最后一条评论:原始问题想要打开文件“C:\002.jpg
”。你不能这样做。您的网站将位于一个服务器上(带有自己的C:驱动器),并且无法直接访问用户自己的C:驱动器。因此,您最好使用像我的代码这样的代码来访问网络驱动器上的文件。
出于安全原因,Chrome专门阻止本地文件访问。
以下是一个在Chrome中解除该标记的方法(但这会使您的系统容易受到漏洞攻击):
有一个解决办法,可以使用Web Server for Chrome。
以下是步骤:
现在可以轻松访问您本地的文件:
function run(){
// 8887 is the port number you have launched your serve
var URL = "http://127.0.0.1:8887/002.jpg";
window.open(URL, null);
}
run();
提示:如果您遇到任何跨域访问错误,可能需要从高级设置中选择CORS标头选项。
当我使用PHP作为服务器端语言时,出现了这个问题,解决方法是在将结果发送给客户端之前生成图像的base64编码。
$path = 'E:/pat/rwanda.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
由于安全原因,Google Chrome不允许加载本地资源。Chrome需要http URL。Internet Explorer和Edge允许加载本地资源,但是Safari、Chrome和Firefox不允许加载本地资源。
转到文件位置并从那里启动Python服务器。
python -m SimpleHttpServer
然后将该URL放入函数中:
function run(){
var URL = "http://172.271.1.20:8000/" /* http://0.0.0.0:8000/ or http://127.0.0.1:8000/; */
window.open(URL, null);
}
python -m http.server
。 - andybegaphp -S localhost:8001
const url = "file:///C:\002.jpg"
chrome.tabs.create({url, active:true})
{
"name": "",
"manifest_version": 3,
"permissions": [
"activeTab",
"tabs"
],
// ...
}
<input type=file>
来访问本地资源。 - dandavis