Skip to content

Commit

Permalink
Fix image file input
Browse files Browse the repository at this point in the history
  • Loading branch information
RestoreMonarchy committed Jun 5, 2022
1 parent e7bc4ad commit ddb1c00
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public async Task<IEnumerable<PlayerWeaponSkin>> GetPlayerWeaponSkinsAsync(strin
public async Task<WeaponSkin> GetWeaponSkinAsync(int weaponSkinId)
{
const string sql = "SELECT ws.*, w.* FROM dbo.WeaponSkins ws JOIN dbo.Weapons w ON w.Id = ws.WeaponId " +
"WHERE ws.Id = weaponSkinId;";
"WHERE ws.Id = @weaponSkinId;";
return (await connection.QueryAsync<WeaponSkin, Weapon, WeaponSkin>(sql, (ws, w) =>
{
ws.Weapon = w;
Expand Down
55 changes: 0 additions & 55 deletions src/web/UnturnedStrikeWebBlazor/Shared/Components/InputFile.razor

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="">
<InputFile OnChange="HandleChange" accept="@Accept" />
<label>Choose file</label>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using System;
using System.Net.Http.Headers;

namespace UnturnedStrikeWebBlazor.Shared.Components
{
public partial class InputImageFile
{
[Parameter]
public int? FileId { get; set; }
[Parameter]
public EventCallback<int?> FileIdChanged { get; set; }

[Parameter]
public string Accept { get; set; }

[Inject]
private HttpClient HttpClient { get; set; }

private async Task HandleChange(InputFileChangeEventArgs args)
{
IBrowserFile file = args.File;
file = await file.RequestImageFileAsync("image/png", 800, 800);

HttpRequestMessage msg = new(HttpMethod.Post, $"api/files")
{
Content = new MultipartFormDataContent()
};

var content = new StreamContent(file.OpenReadStream(30 * 1024 * 1024));
content.Headers.ContentType = MediaTypeHeaderValue.Parse(file.ContentType);
(msg.Content as MultipartFormDataContent).Add(content, "file", file.Name);
var response = await HttpClient.SendAsync(msg);

if (response.IsSuccessStatusCode)
{
FileId = Convert.ToInt32(await response.Content.ReadAsStringAsync());
await FileIdChanged.InvokeAsync(FileId);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
</div>
<div class="form-group">
<label>Image</label>
@*<InputFile @bind-FileId="ModelWeapon.ImageFileId" Accept="image/jpeg,image/png" />
<InputImageFile @bind-FileId="ModelWeapon.ImageFileId" Accept=".png" />
@if (ModelWeapon.ImageFileId != default)
{
<img src="api/files/@ModelWeapon.ImageFileId" style="max-height: 128px;" />
}*@
}
</div>
<div class="form-group">
<label>Category</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
</div>
<div class="form-group">
<label>Image</label>
@*<InputFile @bind-FileId="ModelWeaponSkin.ImageFileId" Accept="image/jpeg,image/png" />
<InputImageFile @bind-FileId="ModelWeaponSkin.ImageFileId" Accept=".png" />
@if (ModelWeaponSkin.ImageFileId != default)
{
<img src="api/files/@ModelWeaponSkin.ImageFileId" style="max-height: 128px;" />
}*@
}
</div>
<div class="form-group">
<label>Rarity</label>
Expand Down

0 comments on commit ddb1c00

Please sign in to comment.