forked from wp-net/WordPressPCL
-
Notifications
You must be signed in to change notification settings - Fork 0
Working with Media
Thomas Pentenrieder edited this page Dec 24, 2020
·
4 revisions
Here is a list of methods and examples of working with Media
// returns all media
var media = await client.Media.GetAll();
// returns media by ID
var media = await client.Media.GetByID(123);
Create parametrized request
// returns result of query
var queryBuilder = new MediaQueryBuilder();
queryBuilder.PerPage = 40;
queryBuilder.Page = 2;
queryBuilder.Before = DateTime.Now;
var media = await client.Pages.Query(queryBuilder);
.Net Standard 1.1-1.3 doesn`t support file manipulation (read or write). So only way to send file content is to create Stream with file content manually. Applicable for .netcore 1.0 apps
// returns created media
// for create media item you must read them to Stream. Media items can be audio, video, image, pdf ot any othe type supported by wordpress
Stream s = File.OpenRead("pathToMedia/media.jpg");
if (await client.IsValidJWToken())
{
var createdMedia = await client.Media.Create(s,"media.jpg");
}
.Net Standard 2.0 supports files manipulation. You can send media files by passing its full path to file. Applicable for .netcore 2.0 apps
// returns created media
// for create media item you must read them to Stream. Media items can be audio, video, image, pdf ot any othe type supported by wordpress
if (await client.IsValidJWToken())
{
var createdMedia = await client.Media.Create(@"C:\pathToFile\media.jpg","media.jpg");
}
// returns updated media
var media= client.Media.GetByID(123);
media.Title.Raw = "New Title";
if (await client.IsValidJWToken())
{
var updatedMedia = await client.Media.Update(media);
}
// returns result of deletion
if (await client.IsValidJWToken())
{
var result = await client.Media.Delete(123);
}