Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将图像从文件发送到控制器_Javascript_Jquery_Asp.net Mvc_Image_Asp.net Mvc 4 - Fatal编程技术网

Javascript 如何将图像从文件发送到控制器

Javascript 如何将图像从文件发送到控制器,javascript,jquery,asp.net-mvc,image,asp.net-mvc-4,Javascript,Jquery,Asp.net Mvc,Image,Asp.net Mvc 4,我使用的是MVC4,我使用的是文件类型 <input type="file" style="height:25px;width:250px;font-style:normal" name="Image" value=" " data-bind="value: applicationiconlogo" id="txtapplicationiconlogo" /> 是否可以从客户端JS文件上的路径获取图像,并将其转换为bytes/base64字符串并传递给控制器。我尝试了以下内容,效果

我使用的是MVC4,我使用的是文件类型

<input type="file" style="height:25px;width:250px;font-style:normal" name="Image" value=" " data-bind="value: applicationiconlogo" id="txtapplicationiconlogo" />

是否可以从客户端JS文件上的路径获取图像,并将其转换为bytes/base64字符串并传递给控制器。

我尝试了以下内容,效果良好

以下是我的看法:

@model NMC.Plugin.Platform.Models.NHIDModel
@using NMC.Common;
@using System.Web.Mvc.Html;
@using System.Web.Mvc;
@using Res = NMC.Plugin.Platform.Resources;

@{
    Layout = "~/Views/Shared/NMCChildView.cshtml";
    NMCSession session = (NMC.Common.NMCSession)Session["NMCSession"];
}

@section Header {
    <script src="~/Scripts/Platform/Utilities/NHIDDetails.js" type="text/javascript"></script>

    <script type="text/javascript">
        var detailsNHID = new DetailsNHID();
        var NHIDDetailsLoadedFirstTime = true;
        function SaveNHIDDetailsComplete() {
            try {
                if (NHIDDetailsLoadedFirstTime == true) {
                    NHIDDetailsLoadedFirstTime = false;
                    return;
                }
                if ($("#iframeNHIDDetails").length != 0) {
                    var response = $($("#iframeNHIDDetails")[0].contentWindow.document.body.children['jsonResult']).text().trim();
                    var resultData = $.parseJSON(response);
                    if (typeof resultData.IsCustomError !== 'undefined') {
                        NMCApp.handleNMCException(response);
                    }
                    else {
                        detailsNHID.handleSaveCallback(resultData);
                    }
                }
            }
            catch (error) {
                NMCApp.showNMCExceptionWindow(error);
            }
        }
    </script>
}

<div class="nmc-tabheader">
    @if (Model.IsNew) { 
        @Res.Utilities.AddNHID
    }
    else
    {
      @Res.Utilities.DetailsNHID 
    }

</div>
@using (Html.BeginForm("SaveNHID", "PlatformUtilities", FormMethod.Post,
                    new
                    {
                        enctype = "multipart/form-data",
                        id = "NHIDDetailsForm",
                        name = "NHIDDetailsForm",
                        target = "iframeNHIDDetails"
                    }))
                {
<fieldset class="search-fieldset" style="width:950px">
    @Html.HiddenFor(m => m.IsNew)
    @Html.HiddenFor(m=>m.ApplicationGuid)
    <div class="nmc-largetab" style="width:910px;margin:5px 5px 0px 5px !important;">
        <div class="nmc-lefttab" style="width:220px">
            <label class="nmc-label">@Res.Utilities.ApplicationName</label>
        </div>
        <div class="nmc-righttab" style="width:250px">
            @Html.NMCTextBoxFor(m => m.ApplicationName, new { id = "txtapplicationName",maxlength = 50, style = "width:245px", @class = "nmc-text", data_bind = "value:applicationName,valueUpdate: 'input'" }, false)
        </div>
        <div class="nmc-lefttab" style="width:150px">
            <label class="nmc-label">@Res.Utilities.Partner</label>
        </div>
        <div class="nmc-righttab" style="width:250px">
            @Html.DropDownListFor(m => m.PartnerGuid, new SelectList(Model.Partners, "Code", "Name"), new { id = "txtpartnernames", @class = "nmc-select", data_bind = "value:partner,valueUpdate: 'input'", })
        </div>
    </div>
    <div class="nmc-largetab" style="width:910px;margin:5px 5px 0px 5px !important;">
        <div class="nmc-lefttab" style="width:220px"><label class="nmc-label">@Res.Utilities.ApplicationDescription</label></div>
        <div class="nmc-righttab" style="width:250px">
            @Html.NMCTextBoxFor(m => m.ApplicationDescription, new { id = "txtapplicationDescription",maxlength = 4000, style = "width:245px", @class = "nmc-text", data_bind = "value:applicationDescription,valueUpdate: 'input'" }, false)
        </div>
        <div class="nmc-lefttab" style="width:150px"><label class="nmc-label">@Res.Utilities.Applicationiconlogo</label></div>
        @if (Model.IsNew)
        {
            <div class="nmc-righttab" style="width:250px">
            <input type="file" style="height:25px;width:250px;font-style:normal" name="logoImage" value=" " data-bind="value: applicationiconlogo" id="txtapplicationiconlogo" />
            </div>
        }
        else
        { 
        <div class="nmc-righttab" style="width:250px">

            <input type="image"  id="image64string"style="border-radius:5px;border:1px solid #c2bdbd;float:left" src="data:image/png;base64,@Model.ApplicationiconLogo" width="30" height="30" />
            <input class="btn btn-primary nmc-button" type="button" value="Change" data-bind="click:enableEidtImage,visible:showChangeImgBtn" style="margin-bottom:22px;float:left" />
            <input type="file" style="height:25px;width:215px;font-style:normal;float:left;margin-top:3px;margin-left:5px" name="logoImage" data-bind="value: applicationiconlogo,visible:editImage" id="txtapplicationiconlogo" />
        </div>
        @Html.HiddenFor(m => m.ApplicationiconLogo)
        }
    </div>
    <div class="nmc-largetab" style="width:910px;margin:5px 5px 0px 5px !important;">
        <div class="nmc-lefttab" style="width:220px"><label class="nmc-label">@Res.Utilities.ApplicationzeroconfigURLscheme</label></div>
        <div class="nmc-righttab" style="width:250px">
            @Html.NMCTextBoxFor(m => m.ApplicationzeroconfigURLscheme, new { id = "txtapplicationzeroconfigURLscheme", maxlength = 255, style = "width:245px", @class = "nmc-text", data_bind = "value:applicationzeroconfigURLscheme,valueUpdate: 'input'" }, false)
        </div>
        <div class="nmc-lefttab" style="width:150px"><label class="nmc-label"></label>@Res.Utilities.ApplicationStatus</div>
        <div class="nmc-righttab" style="width:250px">
        @Html.NMCDropDownListFor(m => m.Status, new SelectList(Model.GetAllStatus(), "Value", "Text"), new { id = "applicationStatus", @class = "nmc-select", data_bind = "value:applicationStatus" }, false)</div>
    </div>
    <div class="nmc-largetab" style="width:910px;margin:5px 5px 0px 5px !important;visibility:hidden">
        <div class="nmc-lefttab" style="width:220px"><label class="nmc-label">@Res.Utilities.AppStoreURLs</label></div>
        <div data-bind="value:applicationStatus" class="nmc-righttab" style="width:250px">
            <input class="nmc-text" type="text" style="width:250px;" id="txtappStoreURLs" data-bind="value: appStoreURLs" />
        </div>
    </div>
    <div class="nmc-largetab" style="width:900px;margin-top:10px;text-align:center">
        <input type="submit" class="btn nmc-button" value="@Res.CommonStrings.Save" data-bind="click: saveNHID,enable: canSaveNHID,css: { 'btn-primary': (canSaveNHID() == true) }" />
        <input type="button" class="btn btn-primary nmc-button" value="@Res.CommonStrings.Cancel" data-bind="click:closeNHID" />
    </div>
</fieldset>
}
<iframe id="iframeNHIDDetails" name="iframeNHIDDetails" onload="SaveNHIDDetailsComplete();" style="position: absolute; left: -999em; top: -999em;"></iframe>
这是我的控制器代码:

[HttpPost]
public async Task<WrappedJsonResult> SaveNHID(NHIDModel model, HttpPostedFileBase logoImage)
        {
}
[HttpPost]
public async Task<WrappedJsonResult> SaveNHID(NHIDModel model, HttpPostedFileBase logoImage)
        {
}