Telerik upload image

HTML:
@if (File.Exists(Server.MapPath(faviconPath)))
                    {
                        <img src="@Url.Content(faviconPath)[email protected]" id="favicon-preview" />
                    }
                    @(Html.Telerik().Upload()
                            .Name("Favicon")
                            .Multiple(false)
                            .Async(async => async
                            .Save("FaviconUpload", "SiteSetting")
                            .Remove("FaviconRemove", "SiteSetting")
                            .AutoUpload(true)
                    ).ClientEvents(i => i.OnUpload("onUploadFavicon").OnSuccess("onFaviconSuccess")))



Javascript;
<script type="text/javascript">
    function onUploadFavicon(e) {
        $.each(e.files, function () {
            if (this.extension != '.ico') {
                showError('@MessageResource.OnlyICO')
                e.preventDefault();
                return false;
            }
        });
    }


    function onFaviconSuccess(e) {
        var preview = $('#favicon-preview');
        if (e.operation == 'remove') {
            preview.hide();
        } else {
            preview.attr('src', '@Url.Content(CurrentSite.GetDataFolderPath())/temp.ico?' + Math.random());
            preview.show();
        }
    }
</script>



Controller:
[HttpPost]
        public void FaviconUpload(HttpPostedFileBase favicon)
        {
            favicon.SaveAs(GetTempFaviconPath());
        }


        public void FaviconRemove()
        {
            var path = GetTempFaviconPath();
            if (System.IO.File.Exists(path))
            {
                System.IO.File.Delete(path);
            }
        }


        private string GetTempFaviconPath()
        {
            return Path.Combine(Server.MapPath(CurrentSite.GetDataFolderPath()), "favicon.ico");
        }

你可能感兴趣的:(upload)