Skip to content

Instantly share code, notes, and snippets.

@davepermen
Created August 2, 2023 16:01
Show Gist options
  • Save davepermen/2ee19d7f43aced8eb3ccc0454de66bba to your computer and use it in GitHub Desktop.
Save davepermen/2ee19d7f43aced8eb3ccc0454de66bba to your computer and use it in GitHub Desktop.
@using Microsoft.AspNetCore.Components.Forms
<AuthorizeView>
<Authorized>
<form method="post" enctype="multipart/form-data" action="/user/update-profile-picture">
@{
var user = context.User?.Identity?.Name ?? "--noname--";
var profilePicture = $"/user/{user}.jpg?{Random.Shared.Next()}";
}
<label>
<img src="@(TemporaryPicture ?? profilePicture)" />
<InputFile name="file" OnChange=ChangeProfilePicture />
@if (TemporaryPicture == null)
{
<div class="button">change<br>profile picture</div>
}
</label>
<input type="hidden" name="redirectto" value="/">
@if (TemporaryPicture != null)
{
<input type="submit" value="save" />
<input type="reset" value="cancel" @onclick=CancelChanging />
}
</form>
</Authorized>
</AuthorizeView>
@code {
string? TemporaryPicture { get; set; }
async Task ChangeProfilePicture(InputFileChangeEventArgs inputFileChangeEventArgs)
{
var image = await inputFileChangeEventArgs.File.RequestImageFileAsync("image/png", 600, 600);
using Stream imageStream = image.OpenReadStream(1024 * 1024 * 10);
using MemoryStream ms = new();
await imageStream.CopyToAsync(ms);
TemporaryPicture = $"data:image/png;base64,{Convert.ToBase64String(ms.ToArray())}";
}
void CancelChanging()
{
TemporaryPicture = null;
}
}
label {
display: grid;
gap: 0;
margin: 0;
padding: 0;
background: #333;
}
.button, input[type=reset] {
background-color: #222;
color: #ddd;
font-size: inherit;
font-weight: inherit;
font-family: inherit;
transition: all .33s linear;
text-decoration-color: transparent;
text-align: center;
cursor: default;
opacity: initial;
align-items: center;
min-height: 2rem;
}
:is(.button, input[type=reset]):hover {
border-color: #ddd;
text-decoration-color: transparent;
filter: invert();
}
.button {
height: 4rem;
line-height: 1.5rem;
}
label * {
margin-top: 0 !important;
}
::deep input[type=file] {
display: none;
}
img {
place-self: center;
max-width: 8rem;
max-height: 8rem;
object-fit: cover;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment