Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

2.7 KB
Raw Blame
@using Grand.Business.Core.Interfaces.Storage
@using Grand.SharedKernel.Extensions
@model string
@inject IPictureService pictureService
@{
    var random = CommonHelper.GenerateRandomInteger();
    var clientId = "picture" + random;
    var picture = await pictureService.GetPictureById(Model);
    var targetPictureSize = 100;
    var area = ViewContext.RouteData.Values["area"]?.ToString();
    var altText = picture?.AltAttribute ?? "";
}

<partial name="Partials/FineUploader" />

<div id="@(clientId + "value")">
    <input asp-for="@Model" type="hidden"/>
</div>
<div id="@(clientId + "image")">
    <img alt="@altText" src="@(await pictureService.GetPictureUrl(Model, targetPictureSize))" />
</div>
@if (picture != null)
{
    <span id="@(clientId + "remove")" class="btn btn-outline-danger btn-sm mt-2">@Loc["Admin.Picture.RemovePicture"]</span>
}
else
{
    <span id="@(clientId + "remove")" class="btn btn-outline-danger btn-sm mt-2" style="display:none;">@Loc["Admin.Picture.RemovePicture"]</span>
}
<br/>
<div id="@clientId"></div>

<div id="@(clientId)-qq-template" style="display: none;">
    <partial name="Partials/QqUploader" />
</div>

<script>
    $(document).ready(function() {

        $("#@(clientId)").fineUploader({
            request: {
                endpoint: '@(Url.Action("AsyncUpload", "Picture", new { area }))',
                params: {
                    Reference: '@ViewData["Reference"]',
                    ObjectId: '@ViewData["ObjectId"]'
                },
                inputName: "file",
                customHeaders: {
                    'X-CSRF-TOKEN': $('input[name="__RequestVerificationToken"]').val()
                }
            },            
            template: "@(clientId)-qq-template",
            multiple: false,
            validation: {
                itemLimit: 5,
                acceptFiles: 'image/*',
            }
        }).on("complete", function(event, id, name, responseJSON, xhr) {
            if (responseJSON.success) {
                $("#@(clientId + "image")").html("<img src='" + responseJSON.imageUrl + "' width='100' >");
                $("#@(clientId + "value") input").val(responseJSON.pictureId);
                $("#@(clientId + "remove")").show();
            }
            else {
                $('.qq-upload-status-message').text(responseJSON.message);
            }
        });

        $("#@(clientId + "remove")").click(function(e) {
            $("#@(clientId + "image")").html("<img src='@await pictureService.GetDefaultPictureUrl(targetPictureSize)' width='100' >");
            $("#@(clientId + "value") input").val('');
            $(".qq-upload-list").empty();
            $(this).hide();
        });
    });
</script>