cdvfile://的URL错误不受支持 [英] Unsupported URL error for a cdvfile://

查看:130
本文介绍了cdvfile://的URL错误不受支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

在我的应用中,我想通过cordova wkwebview访问图像.HTML元素如下所示.

In my app, I want to access an image through cordova wkwebview. The HTML element looks as follows.

<img src="cdvfile://localhost/library-nosync/MyFolder/file.jpg">

在加载此文件时,出现错误无法加载资源:不支持的URL".我正在使用iOS 10.2.

While loading this, I get error "Failed to load resource: unsupported URL". I am working with iOS 10.2.

已验证/尝试的事情:

如果选中了文件夹"MyFolder"下"cordova.file.dataDirectory"中存在的文件列表,我确实会看到其中存在"file.jpg".它具有本机URL作为文件:///var/mobile/Containers/Data/Application/app_id/Library/NoCloud/MyFolder/file.jpg.

If list of files present in "cordova.file.dataDirectory" under folder "MyFolder" is checked, I do see the "file.jpg" present there. It has the native URL as file:///var/mobile/Containers/Data/Application/app_id/Library/NoCloud/MyFolder/file.jpg.

我在内容安全策略中添加了"img-src'self'cdvfile:".

I have added "img-src 'self' cdvfile: " to the Content-Security-Policy.

我在config.xml中添加了以下内容

I have added following in the config.xml

<access origin="cdvfile://*" /> 
<allow-navigation href="cdvfile://*" /> 
<allow-intent href="cdvfile://*" />
<preference name="iosPersistentFileLocation" value="Compatibility" />
<preference name="iosExtraFilesystems" value="library,library-nosync,documents,documents-nosync,cache,bundle,root" />

URL中没有与该错误有关的线程中提到的特殊(非ASCII)字符.还有什么可能是"URL不支持"的原因?.

There are no special (non-ASCII) characters in the URL as mentioned in threads relating to this error. What else could be the reason of "Unsupported URL"?.

我访问cdvfile://路径的方式不正确吗?

Is the way I am accessing cdvfile:// path incorrect?

更新

我碰到一个链接(忘了捕捉),说Webview需要相对路径,因此cdvfile://将不起作用.我试图通过将图像源更改为"../../../../../../../../..//var/mobile/Containers/数据/应用程序/app-id/Library/NoCloud/MyFolder/file.jpg",现在我看到一个新错误-无法加载资源:该操作无法完成.不允许该操作"

I came across a link (forgot to capture that) saying the webview needs relative path and hence cdvfile:// would not work. I tried to change the image source to a relative path by changing it to "../../../../../../../../..//var/mobile/Containers/Data/Application/app-id/Library/NoCloud/MyFolder/file.jpg" and I could now see a new error - "Failed to load resource: The operation couldn’t be completed. Operation not permitted"

我可以通过读取"文件的内容并将该base64数据作为图像源传递来使图像工作.但这不是应该的吗?

I could get the image working by "reading" the contents of the file and passing that base64 data as image source. But that is not how it should be, should it?

推荐答案

如果使用最新的ios平台(cordova-ios@6.x.x),则可以使用方案和主机名选项:

If you use the newest ios platform (cordova-ios@6.x.x) you can use the scheme and host name options:

<preference name="scheme" value="app" />
<preference name="hostname" value="localhost" />

然后,您应该获取并使用 schemeUrl ( app://)而不是 cdvfile:// file://网址.

Then you should get and use the schemeUrl (app://) instead of the cdvfile:// or file:// url.

var cdvfileUrl = "cdvfile://localhost/library-nosync/MyFolder/file.jpg";

// Convert to native url
window.resolveLocalFileSystemURL(cdvfileUrl, function(entry) {
    var nativeUrl = entry.toNativeURL(); // will be "file://...."

    // Use nativeUrl to get scheme friendly url
    var schemeUrl = window.WkWebView.convertFilePath(nativeUrl);  // Will be "app://..."
});

您可以在您的< img> src标记中使用此schemeUrl.

You can use this schemeUrl in your your <img> src tag.

这篇关于cdvfile://的URL错误不受支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆