How to convert string base 64 image js to image in c#?

╄→尐↘猪︶ㄣ 提交于 2021-02-11 13:21:40

问题


i want pass base 64 string image to server site then convert to image in c#

function readFile(input) {
        CheckFileUpload(input.files.length)
        if (input.files && input.files[0]) {
            var img, inputID = 0;
            for (var i = 0; i < input.files.length; i++) {
                var reader = new FileReader();
                reader.onload = (function (f) {
                    return function (e) {
                        //<input type="file" id="file_' + inputID + '" name="file_' + inputID + '" class="d-none" value="' + f + '" style="display:none" />';
                        var fileInput = $('<input/>')
                            .attr('type', "hidden")
                            .attr('name', 'file_' + inputID + '')
                            .attr('id', 'file_' + inputID + '')
                            .attr('value', e.target.result);
                        $('#imgZone').append(fileInput);
                        inputID++;
                    }

                })(input.files[i]);
                reader.readAsDataURL(input.files[i]);
            }
        }

C# Code

var filesRequest = Request.Form;
        string file = filesRequest["file_1"];
        byte[] bytes = Convert.FromBase64String(file); // Error (1) here 
        System.Drawing.Image image;
        using (MemoryStream ms = new MemoryStream(bytes))
        {
            image = System.Drawing.Image.FromStream(ms);
        }

Error (1) : System.FormatException: 'The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. '

base 64 string

来源:https://stackoverflow.com/questions/59823517/how-to-convert-string-base-64-image-js-to-image-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!