Search code examples
firefox-addonfile-uri

Convert URI to File URI (file:////)


In my addon I'm allowing people to browse to an image on their disk. When they do this the file path returned (using the inline options file control) is in form of C:\blah\blah\blah.png. I then take this file path and set a stylesheet using this path: list-style-image: url("' + newValRep + '")

However how can I convert this in a proper way to to file:////C:/blah/blah/blah.png because if i use the first way it doesnt work in my css style sheet.

Currently I'm doing this regex: var newValRep = 'file:///' + newuri.replace(/\\/g, '/');

but this is definitely not the proper way as it is windows specific.

I have tried:

  • new FileUtils.File(newuri).path but this does not give file:/// form
  • OS.Path.normalize(newuri) but same problem
  • Services.io.newURI(newuri, null, null).spec but same issue

Solution

  • You were so close :-)

    var uri = Services.io.newFileURI(somensILocalFile)
    console.log(uri.spec)