Don't miss out Virtual Happy Hour today (April 26).

Try our conversational search powered by Generative AI!

Send asset image to Service Api without FileStream

Vote:
 

Hi!

I am trying to send an image to Episerver through the Service API (/episerverapi/commerce/import/assets).

The way shown below from the example here https://github.com/episerver/ServiceApi-Client/blob/master/EPiServer.ServiceApi.Client/Tests/Catalog/ImportTest.cs works.

private IEnumerable<JobMessage> MediaImportTest(out Guid taskId)
{
	var content = new MultipartFormDataContent();
	var filestream = new FileStream(Path.Combine(TestDataDirectory, "MediaImport.zip"), FileMode.Open);
	content.Add(new StreamContent(filestream), "file", "MediaImport.zip");
	return PostImport("/episerverapi/commerce/import/assets", content, out taskId);
}

However, my code that initiates the POST to episerver is running in a cloud environment where I do not have access to disk/filesystem. Thus I will need to do this without using an ordinary FileStream. I have tried to do it with a memorystream instead (see below code) but no image is added in Episerver.
(note: It works in a test setup if I convert the memorystream to a filestream and then send it, so the content of the memorystream seems to be correct)

using (var memoryStream = new MemoryStream())
{
	using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
	{
		var demoFile = archive.CreateEntry("MediaImport.xml");

		using (var entryStream = demoFile.Open())
		using (var streamWriter = new StreamWriter(entryStream))
		{
			streamWriter.Write("<MediaImport><ContentFolderUri>default content folder uri</ContentFolderUri><MediaType name=\"Image\"><ContentType>Company.Site.Areas.Shared.Media.Image, Company.Site</ContentType></MediaType><MediaGroup mediaTypeName=\"Image\"><Media><IntegrationId>TEST_IMAGE</IntegrationId><Name>Test Media with Binary Content in external URL</Name><BlobContent><ExternalUri>https://whatever/image.png</ExternalUri></BlobContent></Media></MediaGroup></MediaImport>");
		}
	}
	content.Add(new StreamContent(memoryStream), "file", "MediaImportHttp.zip");
	return PostImport("/episerverapi/commerce/import/assets", content, out taskId);
}

When DotPeeking what the service API does on the recieving side I see this:

MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(path);
MultipartFormDataStreamProvider dataStreamProvider = await request.Content.ReadAsMultipartAsync<MultipartFormDataStreamProvider>(provider);
multipartFileData = provider.FileData.FirstOrDefault<MultipartFileData>();

Is there some way I can send images to Episerver through the service API without using a actual file/filestream on the sender side?

Thanks

// Patrik

#231123
Edited, Nov 19, 2020 11:27
Vote:
 

Hi Patrik

I noticed that you are not resetting the MemoryStream.Position to 0 in your code sample.

Try adding memoryStream.Position = 0, just before the content.Add line.

#231139
Nov 19, 2020 17:00
Vote:
 

Holy Badowly, it actually works! Thanks Stefan, you saved my day.

Here is a working code snippet for sending images to Episerver thorugh the Service API without access to disk/file on sending side:

            using (var memoryStream = new MemoryStream())
            {
                using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
                {
                    var demoFile = archive.CreateEntry("MediaImport.xml");

                    using (var entryStream = demoFile.Open())
                    using (var streamWriter = new StreamWriter(entryStream))
                    {
                        streamWriter.Write("<MediaImport><ContentFolderUri>default content folder uri</ContentFolderUri><MediaType name=\"Image\"><ContentType>Company.Site.Areas.Shared.Media.Image, Company.Site</ContentType></MediaType><MediaGroup mediaTypeName=\"Image\"><Media><IntegrationId>TEST_IMAGE</IntegrationId><Name>Test Media with Binary Content in external URL</Name><BlobContent><ExternalUri>https://static.starcraft2.com/dist/images/global/logos/img-sc2-logo--large.png</ExternalUri></BlobContent></Media></MediaGroup></MediaImport>");
                    }
                }

                memoryStream.Position = 0;
                content.Add(new StreamContent(memoryStream), "file", "MediaImportHttp.zip");
                return PostImport("/episerverapi/commerce/import/assets", content, out taskId);
            }

// Patrik

#231140
Edited, Nov 19, 2020 17:22
Stefan Holm Olsen - Nov 19, 2020 17:24
You're welcome, Patrik. Happy it worked.
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.