Svelte path to a static folder - path

I use Sveltekit and put some images in my static folder. Static > images > image1.png etc.
Within source, src, I have a folder called lib and inside lib a folder called components and in there a file Footer.svelte. In that file I want to reference to my image1 (which has an absolute path of ~/myapp/static/images/image1.png. Even when I try this path it throws me an error.
So:
src > lib > components > Footer.svelte
static > images > image1.png
Both src and static are in root dir of myapp.
And this line in my Footer.svelte:
import Image from '~/myapp/static/images/image1.png';
Whats the correct line in my Footer.svelte for grabbing image1.png?
See in problem description.

Files in static are not supposed to be imported, they should be
available as static files directly on the root of the application so you can reference them as:
<img src="/images/image1.png" />
If you want to import an image, put it in src/lib and import from there via:
import Image from '$lib/.../image1.png';
(Assuming you have a path mapping like this in tsconfig.json:)
"paths": {
"$lib": ["src/lib"],
"$lib/*": ["src/lib/*"],
}

Related

Playwright cant find a located file using a wsl

I want to upload a file using playwright, but as I'm using a wsl, I don't know where I should put the file, for sure not in the windows folder, but where? Cause I tried to put it in the mounted disk in ubunbtu, but it isn't editable, I'm using SetInputFiles().
In the image you can see the file manager that playwright opens, i cant find the node folder in the image.Image
You can keep the file in the project fiolder itself and give relative path for that file. Make one folder in project name it as 'TestData' and copy file in the folder. if file name is test.png then you can give path as give below.
let path = require('path');
let fileToUpload = '/testData/fileUpload/' + fileName;
let absolutePath = path.resolve(process.cwd() + fileToUpload);
await this.page.locator("input[type='file']").setInputFiles(absolutePath);

Loading txt file as an asset for a dart package

In flutter it's easy to load a .txt asset at runtime by specifying it or its folder in the pubspec.yaml file and then loading it with rootBundle. However, i'm working on a pure dart package, and I'm struggling to work out how to get the package to load a .txt file relative to it's own directory structure.
When I use the package in a separate dart command line application i'm working on, the relative path that I specified in one of the package source code files causes an error to be thrown that the txt file doesn't exist. I understand why this error is being thrown, because the relative path is interpreted as being from the command line application's root directory instead of the package's root directory, but i'm unsure of how to solve this without specifying the absolute path for the .txt file. I'd rather not specify the absolute path as it makes the package less portable.
Is there anything similar to flutter's asset loading for a pure dart package?
I think you need the resolveSymbolicLinks or resolveSymbolicLinksSync methods to decode the relative path and then use the resolved path to read the txt file:
import 'dart:io';
void main() async {
String file = '../lib/main.dart';
var path = Uri.parse('.').resolveUri(Uri.file(file)).toFilePath();
print(path);
if (path == '') path = '.';
var resolved = await File(path).resolveSymbolicLinks();
print(resolved);
File(resolved).readAsString().then((String contents) {
print(contents);
});
}

How copy image to data directory in firefox addon

I have a html page which has input field(my panel html). I need get path to image(in my file system) from input and set it to <img src = "path">(in tab html). I know that browser don't allow to get absolute path to file.
Questions:
1) How can I copy image to my resource:data directory to get images relative path and set it to my <img src>?
2) Are there another alternatives to set path in img src to any image in my file system?
Please see Nandu's solution. You cannot set src of an image to a file:// uri.
Option 1
Make your panel.html a privileged page. Add a chrome.manifest file to your addon, and load the page into the panel with your chrome://***/content/*** path and it can now load file uris.
OR register your resource page as an about: page with this - https://developer.mozilla.org/en-US/docs/Custom_about:_URLs
Option2
Create a resource:// for it then set your src to that.
You can create a resource like this:
let res = Services.io.getProtocolHandler("resource").QueryInterface(Ci.nsIResProtocolHandler);
res.setSubstitution("myAddonId/myimg", Services.io.newURI('file://blah/blah/blah.png',null,null));
Then set src to resource://myAddonId/myimg
Option 3
XHR the image set the responseType = 'blob' and then on the resulting blob do URL.createObjectURL and then set the src of the img to that.
To show a file dialog and get file path do this:
var fp = Cc['#mozilla.org/filepicker;1'].createInstance(Ci.nsIFilePicker);
fp.init(Services.wm.getMostRecentWindow('navigator:browser'), 'Pick directory the icon container file should be saved in', Ci.nsIFilePicker.modeOpen);
// fp.appendFilters(Ci.nsIFilePicker.filterAll);
var rv = fp.show();
if (rv == Ci.nsIFilePicker.returnOK || rv == Ci.nsIFilePicker.returnReplace) {
console.log('path to selected file:', fp.file.path);
}// else { // cancelled }
You need to:
1.) include image in the addon (under data folder) ex: data\image\panelimg.jpg
2.) in the panel.html file, you will reference img by it's relative path in reference to html file. ex:
You can only reference assets bundled with the add-on (and not any image in file system)

read file from res folder blackberry

I want to read file from "res" folder on blackberry. The file that i used is a file javascript.
I used this code InputStream in = classs.getResourceAsStream("file.js");. But i get "could not find this path" and I use also
String srcFile = "/res/ressourcesWeb/file.js";
FileConnection srcConn = (FileConnection) Connector.open(srcFile, Connector.READ);
InputStream in = srcConn.openInputStream();
but i got an exception.
Can any one help me to read the file and give me the right path that should I use?
Your res folder has to be inside src folder to be accessed from your code.
src folder is the root folder of your project package. And all folders outside of src folder are invisible for the code at runtime.
Check this post for more details: Blackberry runtime error: FRIDG: could not find img/logo.png
There's file location principle described.
You actually do not need to put your resources under the src folder for them to be accessible from your code.
That is one way to do it, but I don't think it's the best way. Files under the src folder should really be source code, not images, or other resources. For JavaScript resources, it's debatable whether those should be under src or not. Most projects I've seen have used the src folder for only Java source code.
In any case, if you would like to keep your file (or other resources, like images) outside the src folder, you can do so. The BlackBerry plugin for Eclipse actually sets it up like this by default, when you create a new project. There is a res folder at the top level, next to (not under) src.
If you have
src\
src\com\mycompany\myapp\
res\
res\resourcesWeb\
res\resourcesWeb\file.js
Then, you can open the file like this:
String jsPath = "/resourcesWeb/file.js";
InputStream input = getClass().getResourceAsStream(jsPath);
byte [] content = IOUtilities.streamToBytes(input);
String contentAsString = new String(content);
P.S. You also can probably do this:
String jsPath = "/file.js";
InputStream input = getClass().getResourceAsStream(jsPath);
and not specify the path to the resource. Obviously, this will only work if there are no naming conflicts in your resource folders (e.g. you don't have /res/resourcesWeb/file.js and also /res/otherPath/file.js)

enum image resources in monotouch

is there a way to enum programmatically all image resources in a folder?
I want to automate animation images sequence creation process and load images like this:
var images = EnumResouceImages("Images/");
foreach(imgPath in images)
UIImage myImg = UIImage.FromFile(imgPath);
Once you know your application's root directory, e.g. by calling NSBundle.MainBundle.BundleUrl.Path, you can do this using the System.IO types that the base class library (BCL) provides.
E.g. this code:
foreach (var filename in Directory.EnumerateFiles (Path.Combine (NSBundle.MainBundle.BundleUrl.Path, "Images"), "*.png"))
Console.WriteLine (filename);
will print every .png file located in your application's Images/ directory.
You can change the filter (e.g. for jpegs or other formats), the directory name (Images) or the processing (Console.WriteLine) with your own logic.

Resources