Display images from API static folder on the client side in MERN application - reactjs

I've got a MERN application that stores images to MongoDB. however, when trying to read the database and display the image back in the client application I keep getting the broken image icon.
I'm using Multer to successfully upload and store the images in the "/images" folder on the API side. I'm able to SEE the files being uploaded into that folder. I'm also using Path to declare the /images folder as a static folder.
on the client side I'm able to query the posts, get back the appropriate filename in the post.photo attribute and build up the source file location using a hard-coded PF constant (public folder). I'm logging out to the console that URL and when I copy and paste that built URL directly into a browser with the server running I'm able to see the picture successfully.
however, when using the URL for the image "src" I'm getting a broken image icon as shown.
I'll try to include a VERY watered down version of the code showing only relevant parts. any help would be greatly appreciated.
[Server Side][1]
[1]: https://i.stack.imgur.com/HkDbs.jpg
[Client Side][2]
[2]: https://i.stack.imgur.com/fJnXg.jpg
[Console App][3]
[3]: https://i.stack.imgur.com/KSunR.png

After weeks of wrestling with this I was finally able to get images to show in my client app! hopefully this helps someone else who might be having the same issue.
removed Helmet from the server side index.js file (it was part of a video instruction that I was following)
added a space-separated WHITELIST environment variable in the server-side .env file (ex: WHITELIST=http:http://localhost:3000)
then added the following code to my index.js code
code sample
Finally, somewhere along in the process I started getting "net::ERR_UNSAFE_PORT" errors showing up in my console. I wound up having to explicitly note my server ports like this "--explicitly-allowed-ports=5000,6000" in my browsers shortcut property by following the instructions noted here
https://superuser.com/questions/188006/how-to-fix-err-unsafe-port-error-on-chrome-when-browsing-to-unsafe-ports
that seemed to fix the issue for me!

Related

Adding local image in Jupyter SQL notebook

Started using Azure data studio and creating documentation for some SQL processes in our system.
As part of that I need to include a flow diagram in the document. Currently I have uploaded an image to a URL and am able to embed the image in document from the URL.
But is there any way to include an image from a local folder? Please assist.
I think this is what you want.
from IPython.display import Image
Image(filename='C:/your_path/your_image.PNG',width=200, height=100)
I was interested in this also. Syntax should be (if you store images in subfolder relative to notebook):
![ssms_subquery](images/16_ssms_subquery.png)
This didn't work for me initially: rendered as a broken image icon.
Things I tried:
Make sure you have a workspace defined for your root folder. Create the folder, then select "File > Add Folder to Workspace ..."
If that does nothing, try closing and restarting ADS. I've found the rendering to be flaky from time to time after awhile or if you have been opening/closing notebooks a lot.
Related topic has been discussed previously for markdown in general (but worth asking again for ADS!), eg. How to display local image in markdown

How to show Image from local drive in Angular JS [duplicate]

I'm trying to retrieve a picture from my file system after a good storage,(instead of putting it in the database I copy it to the disc and i put the path to the db)
I had store the picture to c:\images\ folder and supposing that the name the complete path is c:\images\mypic.jpg
when I try to retrieve it a set the img src attribute to <img src="c:\images\mypic.jps"> by using some java code
in the browser console I found this error Not allowed to load local resource: file:///C://images//mypic.jpg
Question: how to fix these path problem ? where Should I store the pictures ? and from where should I retrieve them ?
sending tag <img src="c:\images\mypic.jpg"> would cause user browser to access image from his filesystem.
if you have to store images in folder located in c:\images i would suggest to create an servlet like images.jsp, that as a parameter takes name of a file, then sets servlet response content to an image/jpg and then loads bytes of image from server location and put it to a response.
But what you use to create your application? is it pure servlet? Spring? JSF?
Here you can find some info about, how to do it.
In Chrome, you are supposed to be able to allow this capability with a runtime flag --allow-file-access-from-files
However, it looks like there is a problem with current versions of Chrome (37, 38) where this doesn't work unless you also pass the runtime flag --disable-web-security
That's an unacceptable solution, except perhaps as a short-term workaround, but it has been identified as an issue:
https://code.google.com/p/chromium/issues/detail?id=379206
You have Two alternatives :
First one is to create a ServletImageLoader that would take as a parameter an identifier of your image (the path of the image or a hash) that you will use inside the Servlet to handle your image, and it will print to the response stream the loaded image from the server.
Second one is to create a folder inside your application's ROOT folder and just save the relative path to your images.
Many browsers have changed their security policies to no longer allow reading data directly from file shares or even local resources. You need to either place the files somewhere that your tomcat instance can serve them up and put a "regular" http url in the html you generate. This can be accomplished by either providing a servlet which reads and provides the file putting the file into a directory where tomcat will serve it up as "static" content.
The concept of http location and disk location is different. What you need to do is:
for uploaded file summer.jpg
move that under a known (to the application) location to disk, e.g c:\images\summer.jpg
insert into db record representing the image with text summer.jpg
to display it use plain <img src="images/summer.jpg" />
you need something (e.g apache) that will serve c:\images\ under your application's /images. If you cannot do this then in step #2 you need to save somewhere under your web root, e.g c:\my-applications\demo-app\build\images
This error means you can not directly load data from file system because there are security issues behind this. The only solution that I know is create a web service to serve load files.
Here is a simple expressjs solution if you just want to run this app locally and security is not a concern:
On your server.js or app.js file, add the following:
app.use('/local-files', express.static('/'));
That will serve your ENTIRE root directory under /local-files. Needless to say this is a really bad idea if you're planning to deploy this app anywhere other than your local machine.
Now, you can simply do:
<img src="/local-files/images/mypic.jps"/>
note: I'm running macOS. If you're using Windows you may have to search and remove 'C:\' from the path string
Do not use ABSOLUTE PATH to refer to the name of the image for example: C:/xamp/www/Archivos/images/templatemo_image_02_opt_20160401-1244.jpg. You must use the reference to its location within webserver. For example using ../../Archivos/images/templatemo_image_02_opt_20160401-1244.jpg depending on where your process is running.

Custom fonts not getting uploaded in Openshift server

I have developed an application which uses custom CSS3 fonts. But somehow custom fonts are not getting uploaded in the server. My application URL is http://techcurt-kshitiz.rhcloud.com and when I try to access custom font like http://techcurt-kshitiz.rhcloud.com/resources/css/LindenHill-webfont.eot, The requested resource is not available appears. Custom fonts are working fine in my local machine in every browser.
I have recreated my application but still the problem persists. I have been using Openshift since an year but never faced such issue. What can be the possible reason for it? Even .less file is not getting uploaded. I am using tomcat 6 server in Openshift. I am not able to find out any solution in Google.
Do you see the files on disk when you SSH to the gear?
Are the files checked into Git? It's possible that they are being ignored by git, and hence not uploaded. You can check that the file is in git with "git show master:" - if ignored it won't show up.
Otherwise, it could be something in your build - perhaps the files are getting deleted?
Finally my issue is solved by servlet-mapping .ttf and .eot files in my web.xml files. But I am not able to understand how come my application was running in local machine but not in server? How come thats possible?

richimage component src value does not work with uri value

When I set uri string value into richimage src property, I can not see the image on application ui.
If I try to paste the value into browser it shows the image
ie: file:/C:/Users/Dijitaluser/AppData/Roaming/JDeveloper/system11.1.1.6.38.61.92/o.j2ee/drs/IncomingPaperWorkWebApp.war/WEB-INF/classes/com/acme/resource/images/upload_file.png
As a matter of fact, if i put the image file into the same project as you say image/thefile.png, that's ok. But I use distributed resources project and I get the uri by using resourceBundle. So this solution is useless for me.
I' m trying to test application on my integrated weblogic server, and this uri which is returned by resourceBundleHelper, is already validated by browser. Could you plz give me some suggestions about the issue.
I trasnformed my resources project to web project to use web.xml about defining resources servlet. Then moved my all images in META-INF/adf/images and added into my VisualResourceBundle(extends ListResourceBundle) class.
It works
http://www.oracle.com/technetwork/developer-tools/adf/learnmore/86-images-from-jar-427953.pdf
hope it helps

Getting images with HTTP Request in C

I am writing a program in C that acts like a proxy server in a Linux system: Client asks it for a web page,
it sends an HTTP GET Request to a distant server, and it gets the servers response (web page), which is saved in an .html file.
Here goes my problem: Most web sites got some references to images, so when i try to view the .html file proxy created, the images don't appear.
I have searched a lot, but found nothing..Is there a way to write some code to GET images too?
Thank you in advance
You're going to have to write code that parses the HTML file you get back and looks for image references (img tags), then queries the server for those image files. This is what web browsers are doing under the hood.
You have an additional problem though which is that the image references in the HTML file are to the original server. I'm assuming that since they don't load for you the server that returned the original HTML isn't available. In that case after you get each image file you will need to give it a name on the local filesystem and then alter the reference in the HTML (programmatically) to point to your new local image name.
So for example:
<img src='http://example.com/image1.png'>
would become
<img src='localImage1.png'>
If you're querying arbitrary websites then you'll also find that there are various other files you'll need to do the same with like CSS files and JavaScript files. In general its hard to mirror arbitrary web pages accurately - browsers have complex object models they use to interpret web pages because they have to deal with things like CSS and Javascript and you may need to be able to 'run' all that dynamic code to even be sure what files to download from the server (e.g. JavaScript including other JavaScript etc).

Resources