Minimal implementation of file upload API implemented by asp.net core #2149
SmRiley
started this conversation in
Show and tell
Replies: 5 comments 4 replies
-
You can use |
Beta Was this translation helpful? Give feedback.
3 replies
-
请问,你能把前端的Razor代码也分享出来吗?:raising_hand: |
Beta Was this translation helpful? Give feedback.
1 reply
-
Minimal API: app.MapGet("/upload", async (IFormFile file) =>
{
var uploads = Path.Combine(uploadsPath, file.FileName);
using var fileStream = File.OpenWrite(uploads);
using var uploadStream = file.OpenReadStream();
await uploadStream.CopyToAsync(fileStream);
}); |
Beta Was this translation helpful? Give feedback.
0 replies
-
没有调用api,节省代码,直接使用b64储存图片(头像) <InputFile OnChange="ResizeAndDisplayImageUsingStreaming" />
<Image Width="200px"
Height="200px"
Src="@newUser.Avatar"
PreviewSrc="@newUser.Avatar"
/>
private async Task ResizeAndDisplayImageUsingStreaming(InputFileChangeEventArgs e)
{
try
{
var imageFile = e.File;
var resizedImage = await imageFile.RequestImageFileAsync("image/png", 250, 250);
MemoryStream ms = new MemoryStream();
await resizedImage.OpenReadStream().CopyToAsync(ms);
var bytes = ms.ToArray();
var b64 = Convert.ToBase64String(bytes);
Console.WriteLine($"b64{b64}");
newUser.Avatar = "data:image/png;base64," + b64;
}
catch (Exception ex)
{
Console.WriteLine($"error{ex}");
}
} ps:图片默认最大500k |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
就目前而言,无论是Server还是Wasm都只能通过WebAPI上传,如果你需要使用Stream进行处理,还是使用官方的InputFile吧.不知道后续是否有实现Stream的计划,毕竟有这个的话在Server Inside上能省去写Controller的麻烦
这里给出使用Asp.Net Core的最小Upload WebApi实现
这个案例里面使用Jwt进行鉴权并且上传文件格式限定为Png,你可以按照自己需求进行替换
For now, both Server and Wasm can only be uploaded via WebAPI. If you need to use Stream for processing, use the official InputFile. I don't know if there are plans to implement Stream in the future. After all, if there is this, it is on Server Inside. Can save the trouble of writing Controller
Here is the implementation of the minimal upload interface implemented by WebApi using Asp.Net Core
In this case, jwt is used as authentication and the upload format is limited to Png, you can replace it according to your needs.
我是菜鸟,如果代码有遗漏或者错误,可以提出或者补充
I am a novice, if there are omissions or errors in the code, you can suggest or add
Beta Was this translation helpful? Give feedback.
All reactions