-
-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Opening images in development errors #99
Comments
I'm having a similar error. When I reference a file which a user has uploaded from a file input field, and try to display the image I get this error:
|
I think these are different problems. Or maybe two sides of the same problem, @holywyvern What OS are you on? A colleague of mine on Mac had to start the dev server with sudo, because of some troubles. Might be similar..? @Circuit8 Do you get this error in development or in production? Because.. In your case you try to load an image via http protocol from localhost.
|
@loopmode Windows 10 standard/pro (I have two PCS on both it has the same problem), 64 bits. I think it may be than the webpack dev server or something may be trying to load it as a relative resource and fails. I tried to do that anyway and ran my powershell as administrator, but the problem is still there. Be aware I'm talking than I'm trying to open an image from a folder outside the /static or the project. Of course, for now it works when I'm building -> runing the project, but I miss all the development features while doing that. |
Well I didn't have much luck with the static folder myself, but loading from documents folder worked nicely.
Actually, after checking actual project, I did not always resolve, so I ended up with stuff like: still it works nicely, So, while mixed forward/backward slashes apparently don't matter much, the |
@loopmode I get it in development. I kind of get what your saying. How then do I select an image on my dev server? |
@Circuit8 Well, you don't.. :) What you can do is to put uploads on the filesystem. Regarding uploads, the simplest thing you can do is - since the user probably picks the image on the very same machine your app is running on? - to just copy a file picked via Some links around this topic: @holywyvern sorry for hijacking your issue here. Strayed off topic again :) |
@loopmode thanks so much for that explanation it's all beginning to make sense now. Cheers! |
You're welcome! Go make a cool app! :) |
I tried to put only file:, but the problem is still there... Here it's what my code is actually doing: components/ImagePreview.jsimport React from "react";
import { format as formatUrl } from "url";
const ImagePreview = ({ src }) => {
return (
<div className="img-preview">
<div className="img-container">
<img src={formatUrl({
pathname: src,
protocol: 'file',
slashes: true
})} />
</div>
</div>
);
};
export default ImagePreview; src is the absolute path to file. I tried changing the formatUrl as: <img src={`file:${src}`} /> But it doesn't work on development, yet again, it does on production. I would brush it off as a computer's problem on my environment, but I have my PC at home wich does the exact same thing. It should be noted, than even using the console fails and it's not a React problem either: let img = new Image();
img.src = require("url").format({
pathname: "C:\Users\RAMIRO\Documents\test.png",
protocol: 'file',
slashes: true
}); |
For those who have the possibility to do so, you can disable some security restriction from Chrome in Electron: const window = new BrowserWindow({
webPreferences: {
webSecurity: false
}
} Note that you should not use this if your app is to be downloaded and install publicly. |
I "solved" it by grabbing the image contents as a buffer, converting to base64 and then displaying the base64 string in the HTML const logoPath = '/path/to/my/image.png';
const logo = fs.readFileSync(logoPath).toString('base64'); <img :src="'data:image/jpg;base64,' + logo" /> It doesn't seem to matter what the mime type is - I've used |
Adding some info to help narrow down the issue. <img src={`file:${icon.location}`}/> When the image tag is shown in the DOM in the dev console it looks like this: <img src="file:C:\Users\Mason\AppData\Roaming\Electron\userImages\tYzBXzcK.bmp"> I get the following error in the console:
But, and here is the kicker, when I hover over the src value in the dev console, or copy the link address from the context menu I get this Note: even though I have prepended the src with |
I resolved my problem by using this: import * as url from 'url';
import { isDevelopment } from './utils';
const src = (val: string) => (isDevelopment ? url.resolve(window.location.origin, val) : path.join(__static, val));
export const Splashscreen = () => <img className="img-middle" height="80px" src={src('./images/logo.png')} />; |
after a bloody battle this worked for me. but also gave me a memory leak since I call this 4 times every second. also I realized later this problem affected also other parts of my app (the app plays videos and they were not working as well). what really solved my issue was this: electron/forge#220 (comment) oh... and that's crazy: the issue for me only happened in production and not in dev... lol my app uses electron-forge so I am not sure the same solution will help you guys, but sending in a hope. thanks! |
Of course this only applies to loading images from |
Closing this issue for now, as there are a couple of ways to solve this problem, including pretty robust ones. As mentioned in my last comment, here is one way to solve the problem:
|
While the helper is a working strategy, I'm not sure I'd call it "excellent". I think the discrepancy between development and production static asset strategies should be fixed rather than worked around, though I see how it would be a very difficult thing to solve because of the differences in how the two environments retrieve content. At the very least, I would say this issue should be referenced in the docs for static assets, if not discussed in some detail in the docs themselves since any notion of this problem is currently absent. |
Or we simply put such a helper (probably somewhat improved) into the library itself, and document it as the way to embed static assets. What do you think? |
I think that'd be good, for a start at least. I'd still ideally like to see some way to just specify a single path-like in the img tag (which would be much more familiar to anyone with web development background), but that's probably not simple if even possible. |
Not quite sure I understand what you mean. Are you suggesting that e.g. |
In an ideal world, yes. Though I'm not sure how viable that would be. I imagine there might be some issues with that since the retrieval is being done by the chromium engine in electron. Perhaps electron-webpacker could process src attributes on html elements though and do something similar to this proposed integrated helper function? I'm not too familiar with the inner workings of it so not sure whether that's possible. |
When I try to read an Image from a file inside my documents folder (e.g. C:/users/me/Documents/test.png) doesn't work, the loader keeps intercepting the file and trying to find it locally on the project directory.
It prints this error:
This problem only applies on development, when the app is on production it works as expected and the image is displayed without trouble.
The text was updated successfully, but these errors were encountered: