-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblazor-upload-file.cs
More file actions
28 lines (25 loc) · 939 Bytes
/
blazor-upload-file.cs
File metadata and controls
28 lines (25 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// obsluha události na <InputFile>
private void LoadFile(InputFileChangeEventArgs arg)
{
MyModel.BrowserFile = arg.File;
}
// uložení na disk, typicky v Blazor Server
if (MyModel.BrowserFile != null)
{
string path = WebHostEnvironment.WebRootPath + $"/images/{Id}.jpg";
await using FileStream fs = new(path, FileMode.Create);
await MyModel.BrowserFile.OpenReadStream().CopyToAsync(fs);
}
// odeslání na REST API
using var content = new MultipartFormDataContent();
using var httpContent = new StreamContent(file.OpenReadStream());
httpContent.Headers.ContentType = new MediaTypeHeaderValue(browserFile.ContentType);
content.Add(httpContent, "files", file.Name);
var response = await _httpClient.PostAsync("api/files", content);
// REST API endpoint
[HttpPost("/api/files")]
public IActionResult UploadFile([FromForm] IEnumerable<IFormFile> files)
{
// nebo...
var allFiles = HttpContext.Request.Form.Files;
}