Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to derive(fake) a location path to an in-memory MemoryStream file?

Is it possible to derive a location path of an in-memory file?

My justification for doing this is based on having a collection of images that are retrieved by my WinForms application in a Base64 encoded string format. I need to build up some HTML and inject these images so that they can be rendered on an embedded page in a WebBrowser control on my application. Since we're talking about HTML here, I need to use the <IMG> tag to display the image. This element needs to accept a "src" path which means I need to determine a method of deriving an absolute/relative path to each of the in-memory images.

like image 781
fin Avatar asked Oct 22 '22 19:10

fin


2 Answers

If you control the application running on the server (which you indicated in a comment), then you should be able to redirect requests for particular resources. For example, if the user application requests "http://myserver/memory/imgxxx.jpg", the server should be able to intercept that and, rather than try to serve imgxxx.jpg from disk, construct an image from the data in memory, and ship it down to the client.

Now, if you're just shipping the base64 encoded data to the client, and want the client to somehow access the data and do the conversion ... that's a harder problem. I can envision doing something with JavaScript to replace all of the img tags that have some given attributes with the corresponding image. But I suspect that'd get pretty messy. If it's even possible.

Another possibility is to create a derived WebBrowser component and customize its behavior. A good example is in the CreateSink method documentation. It might be possible to write a handler that is called whenever the component wants to download something. You could then intercept the call and supply your in-memory image. I'm not certain that this is possible. You might take a look at WebBrowser customization. I will say, though, that it's probably easier to just write the files and use a "file://" url.

like image 89
Jim Mischel Avatar answered Nov 02 '22 04:11

Jim Mischel


You can build a light weight HTTP server into your application by using the HttpListener class.

You will want to use the asynchronous model. Create a url that it serves content to, such as "http://*:8080/appdata", and then use that url within your html (http://localhost:8080/appdata/someinmemoryresource").

When theHttpListener receives a request, look at the path and respond with whatever in memory data it is you want to serve!

like image 40
Chris Pitman Avatar answered Nov 02 '22 05:11

Chris Pitman