Skip to content

[API Proposal]: Add FromFile method to BinaryData #106203

@JoshLove-msft

Description

@JoshLove-msft

Background and motivation

Oftentimes, people want to upload local files using BinaryData. In order to do this with the current API, they must write the following code:

var bd = BinaryData.FromStream(File.OpenRead("foo"));

which is not obvious to many users.
It would be better if they could simply do:

var bd = BinaryData.FromFile("foo");

API Proposal

namespace System.Memory.Data;

public partial class BinaryData
{
    public static BinaryData FromFile(string path);
    public static Task<BinaryData> FromFileAsync(string path, CancellationToken cancellationToken = default);
    public static Task<BinaryData> FromFileAsync(string path, string? mediaType, CancellationToken cancellationToken = default);
}

API Usage

var bd = BinaryData.FromFile("foo");

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Memoryin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions