Import media from url

Hi! In my company, we had two projects using enonic that we needed the creation of a media content using a url instead of a file from multipart-form, for example. If there’s any way I could create this media, please help me. I’ve tried in a controller, do a request with httpClientLib, create a stream with ioLib using the response and then creating a media object. This doesn’t worked. Would be nice for next versions, implement a function like content.createMedia(), but content.createMediaFromUrl() or something like this.


1 Like


Do you have source-code for your example?

Hi @PVMerlo

We are planning to improve stream (binary) support in http-client lib, by adding a bodyStream property on the result of the request function. Then you will be able to pass the result to createMedia data parameter.

It will work something like this:

var response = httpClientLib.request({
    url: 'http://host/image.png',
    method: 'GET'

var responseStream = response.bodyStream; // this field is currently missing 

var mediaResult = contentLib.createMedia({
    name: 'myImage',
    parentPath: '/some/path',
    mimeType: 'image/png',
    data: responseStream

Then it becomes simple to create your own createMediaFromUrl() function.

Hopefully this will be a part of the next 6.6 version.


Nice, @aro! Thanks :smiley:

Stream support in http-client has been implemented and it will be in 6.6

This will be included in the 6.6 version of Enonic XP. Hopefully we will release next week.

1 Like

Here is also an example from ContentHive widget of saving an image from URL by creating a ByteSource in Java and then using createMedia in javascript: