Skip to content

Commit b83f57d

Browse files
authored
Request Streaming upload (#33693)
1 parent 6d8e5f6 commit b83f57d

File tree

3 files changed

+327
-38
lines changed

3 files changed

+327
-38
lines changed

aspnetcore/blazor/call-web-api.md

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,37 @@ You can address this by flowing prerendered state using the Persistent Component
353353

354354
:::moniker-end
355355

356+
:::moniker range=">= aspnetcore-9.0"
357+
358+
## Client-side request streaming
359+
360+
For Chromium-based browsers (for example, Google Chrome and Microsoft Edge) using the HTTP/2 protocol, and HTTPS, client-side Blazor uses [Streams API](https://developer.mozilla.org/docs/Web/API/Streams_API) to permit [request streaming](https://developer.chrome.com/docs/capabilities/web-apis/fetch-streaming-requests).
361+
362+
To enable request streaming, set <xref:Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssemblyHttpRequestMessageExtensions.SetBrowserRequestStreamingEnabled%2A> to `true` on the <xref:System.Net.Http.HttpRequestMessage>.
363+
364+
In the following file upload example:
365+
366+
* `content` is the file's <xref:System.Net.Http.HttpContent>.
367+
* `/Filesave` is the web API endpoint.
368+
* `Http` is the <xref:System.Net.Http.HttpClient>.
369+
370+
```csharp
371+
var request = new HttpRequestMessage(HttpMethod.Post, "/Filesave");
372+
request.SetBrowserRequestStreamingEnabled(true);
373+
request.Content = content;
374+
375+
var response = await Http.SendAsync(request);
376+
```
377+
378+
Streaming requests:
379+
380+
* Require HTTPS protocol and don't work on HTTP/1.x.
381+
* Include a body but not a `Content-Length` header. [CORS](xref:security/cors) with a preflight request is required for cross-origin streaming requests.
382+
383+
For more information on file uploads with an <xref:Microsoft.AspNetCore.Components.Forms.InputFile> component, see <xref:blazor/file-uploads#file-size-read-and-upload-limits> and the example at [Upload files to a server with client-side rendering (CSR)](xref:blazor/file-uploads#upload-files-to-a-server-with-client-side-rendering-csr).
384+
385+
:::moniker-end
386+
356387
## Add the `HttpClient` service
357388

358389
*The guidance in this section applies to client-side scenarios.*
@@ -368,10 +399,7 @@ In the `Program` file, add an <xref:System.Net.Http.HttpClient> service if it is
368399

369400
```csharp
370401
builder.Services.AddScoped(sp =>
371-
new HttpClient
372-
{
373-
BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
374-
});
402+
new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
375403
```
376404

377405
The preceding example sets the base address with `builder.HostEnvironment.BaseAddress` (<xref:Microsoft.AspNetCore.Components.WebAssembly.Hosting.IWebAssemblyHostEnvironment.BaseAddress%2A?displayProperty=nameWithType>), which gets the base address for the app and is typically derived from the `<base>` tag's `href` value in the host page.
@@ -385,10 +413,7 @@ If you're calling an external web API (not in the same URL space as the client a
385413

386414
```csharp
387415
builder.Services.AddScoped(sp =>
388-
new HttpClient
389-
{
390-
BaseAddress = new Uri("https://localhost:5001")
391-
});
416+
new HttpClient { BaseAddress = new Uri("https://localhost:5001") });
392417
```
393418

394419
## JSON helpers

0 commit comments

Comments
 (0)