question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug: convertFileSrc Does Not Work in Web

See original GitHub issue

Bug Report

Capacitor Version

Latest Dependencies:

  @capacitor/cli: 2.4.0
  @capacitor/core: 2.4.0
  @capacitor/android: 2.4.0
  @capacitor/electron: 2.4.0
  @capacitor/ios: 2.4.0

Installed Dependencies:

  @capacitor/android not installed
  @capacitor/ios not installed
  @capacitor/cli 2.4.0
  @capacitor/core 2.4.0
  @capacitor/electron not installed

Platform(s)

Web.

Current Behavior

I followed the sample in the documentation. However, the converted path does not work on the browser showing GET http://localhost:8100/DATA/demo 404 (Not Found).

Expected Behavior

The image should be loaded with the provided path.

Code Reproduction

The minimal sample Ionic project can be found in this repository.

A tiny image is hard-coded as base64 string in the data field when writing the file with Capacitor Filesystem API.

const writeResult = await Filesystem.writeFile({
  data: 'iVBORw0KGgoAAAANSUhEUgAAAAYAAAAECAYAAACtBE5DAAAABHNCSVQICAgIfAhkiAAAABl0RVh0U29mdHdhcmUAZ25vbWUtc2NyZWVuc2hvdO8Dvz4AAAA9aVRYdENyZWF0aW9uIFRpbWUAAAAAADIwMjDlubTlhavmnIgzMeaXpSAo6YCx5LiAKSAyM+aZgjI35YiGMjTnp5IACi9UAAAASElEQVQImWOUk5P7z4AFsEAoSQa/kGYGu2ezGbazuzP4K8jBJBgYGBjYGETVwxiszi9gaDp0A1mCgYHh7U6G6RcuM7xhYGAAAEFTDx/VUsAUAAAAAElFTkSuQmCC',
  directory: FilesystemDirectory.Data,
  path: 'demo',
  recursive: true
});
this.src = Capacitor.convertFileSrc(writeResult.uri);
const readResult = await Filesystem.readFile({
  directory: FilesystemDirectory.Data,
  path: 'demo'
});
this.base64Src = `data:image/png;base64,${readResult.data}`;

The template shows two identical images with different approach: by path and by base64 string.

<img [src]="src" />
<img [src]="base64Src" />

The first image throws a 404 error.

Other Technical Details

npm --version output: 6.14.8

node --version output: v10.16.0

Other dependencies can be found in the sample project.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
seanwu1105commented, Nov 5, 2020

@Gorshtak I try to build apps with Capacitor on native devices. However, it is still convenient for me to fast prototype the look and feel on the browser with ionic serve before applying to the native environment. Furthermore, the official documentation says the Filesystem plugin supports Android, iOS and PWA, which is misleading as the WriteResult.uri is not usable on PWA. This should be at least documented.

2reactions
ghostcommented, Dec 12, 2020

I had similar problem, on android 10 only

so basically for angular:

import { WebView } from '@ionic-native/ionic-webview/ngx';
constructor(private webView: WebView) {}
...
this.webView.convertFileSrc(path)

or you can also follow official ionic way https://forum.ionicframework.com/t/how-to-use-capacitors-convertfilesrc-instead-of-cordovas-convertfilesrc/167283

you dont need to do “http://localhost/” + relative path(i saw on different forums this way of solving), the problem is that if you have live-reloading enabled, convertFileSrc will convert your path to “http://192.168…/” (external adress). If You disable live-reloading, and remove “server”: “{…}” from your capacitor.config.json your convertFileSrc will convert path to “http://localhost/…”

but still, all works fine on 7, 8, 9, 11 android. 10 still throws 404(OK) and if i try to send to backend images that was parsed with this error i will get error in console: EACCESS (Permission Denied)

Solved this by adding ’ android:requestLegacyExternalStorage=“true” ’ in AndroidManifest.xml in tag <application>

Read more comments on GitHub >

github_iconTop Results From Across the Web

window.Ionic.WebView.convertFileSrc not working in ionic 3
Ionic.WebView.convertFileSrc() for Rewriting file:// due to wkwebview. It works fine in android platform but in iOS platform it gives error for ...
Read more >
normalizeURL depricated, cannot get WebView ...
Hi there, I opened my app today and it crashed, saying normalizeURL is depricated use convertFileSrc instead.
Read more >
cordova-plugin-ionic-webview
The Web View plugin for Cordova that is specialized for Ionic apps. ... The issues on GitHub should be reserved for bug reports...
Read more >
Using the Capacitor Filesystem API to Store Photos
All we need to do is run the result through Capacitor.convertFileSrc . which will result in a URI like the following: http://localhost/_ ...
Read more >
cordova-handle-ssl-error
A Web View plugin for Cordova, focused on providing the highest performance ... The issues on GitHub should be reserved for bug reports...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found