umbraco / Umbraco.Headless.Client.Net

.NET Client Library and Samples for the Umbraco headless cms Cloud Service called Umbraco Heartcore
https://umbraco.com/products/umbraco-heartcore/
13 stars 14 forks source link

Media.Create-endpoint broken for "non-image" types? #27

Closed ghost closed 4 years ago

ghost commented 4 years ago

I am using the .Net Core Client Library to create and upload new media items with the Content Management API's Media.Create-endpoint. For some reason this only seems to work for image-files (.png, .jpg...) but not for other types (.txt, .pdf... when MediaTypeAlias is set to "File" instead of "Image").
 Is the API broken? Or is something wrong with the way I am using the API? I have not found any clues in the API-documentation or elsewhere.

Besides my own code I have also tried the provided console-sample-application. It has only been coded to demonstrate how images can be uploaded. But changing the code a bit (MediaTypeAlias and ContentType) produces the same result (Media-items without content).

I have gathered some more details that might be useful in understanding/recreating/analyzing the problem (code-snippet, screenshots from Umbraco Backoffice, Fiddler's capture of request/response for the API-call): Issue creating media items with Content Management API's Media.Create-endpoint.pdf

sitereactor commented 4 years ago

When uploading images the Image media type will be an Image Cropper, which takes an object like is shown in the sample below

var media = new Net.Management.Models.Media {Name = mediaName, MediaTypeAlias = "Image", ParentId = folderId};
media.SetValue("umbracoFile", new { src = fileName }, new FileInfoPart(new FileInfo(imagePath), fileName, $"image/{extension}"));
var image = await managementService.Media.Create(media);

When uploading a PDF try doing this instead

var media = new Net.Management.Models.Media {Name = mediaName, MediaTypeAlias = "File", ParentId = folderId};
media.SetValue("umbracoFile", fileName, new FileInfoPart(new FileInfo(imagePath), fileName, $"application/pdf"));
var image = await managementService.Media.Create(media);

From https://github.com/umbraco/Umbraco.Headless.Client.Net/blob/master/samples/Umbraco.Headless.Client.Samples.Console/Umbraco.Headless.Client.Samples.Console/Program.cs#L178-L204

We can add a sample for uploading a file as well, so it shows both Image and File uploads.

ghost commented 4 years ago

Changing the code as suggested works. Thanks :)