0

net code and I am facing this issue when I am trying to insert string in Binary format.

I Have binary format of image in decal.DecalLogoFileName and decal.DecalLogoFileName is define a string so when I am trying as

cmd.Parameters.Add(new SqlParameter("@DecalLogoFileName", SqlDbType.Binary)).Value=decal.DecalLogoFileName;

decal.DecalLogoFileName="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxITEhUTExIWFhUXGB8aGBcYGR0ZHxobGhoXHRcYGBkaHyghHRolHRcXIjEhJSkrLi4uGB8zODMtNygtLisBCgoKDg0OGhAQGy0lHyUtLS0tLS0tLS0tLS0tLy0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSstLS0tLS0tLS0tLf/AABEIALcBEwMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAAEBQIDBgABB//EAEgQAAIBAgQDBQQHBQYDCAMAAAECEQADBBIhMQVBUQYTImFxMoGRoSNCUrHB0fAUcpLS4QczU2KCshVUc0Njg5OiwuLxFiRk/8QAGQEAAwEBAQAAAAAAAAAAAAAAAQIDAAQF/8QALBEAAgICAgECBQQCAwAAAAAAAAECERIhAzFBE2EiMlFxgUKhsdEEkSMzUv/aAAwDAQACEQMRAD8A"

I am string value in decal as

function readURL(input) {
if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {
            $('#DecalLogo').attr('src', e.target.result);
            $('#hdImageName').val(e.target.result);
        }
        reader.readAsDataURL(input.files[0]);
    }
}

here hdImageName value is store in decal.DecalLogoFileName

I had also tried

cmd.Parameters.Add(new SqlParameter("@DecalLogoFileName", SqlDbType.Binary)).Value=Convert.toByte(decal.DecalLogoFileName);

But Conversion issue is coming.

The column datatype of DecalLogoFileName is set as varbinary(MAX)

4

1 回答 1

1

Try like this;

var byteArray = Encoding.ASCII.GetBytes(decal.DecalLogoFileName);
cmd.Parameters.Add(new SqlParameter("@DecalLogoFileName", SqlDbType.VarBinary, byteArray.Length)).Value=byteArray;

To retrieve the image from database, you can convert it base64 string by using Encoding.ASCII.GetString(byteArray). Then you can put the image in img src like this;

        <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxITEhUTExIWFhUXGB8aGBcYGR0ZHxobGhoXHRcYGBkaHyghHRolHRcXIjEhJSkrLi4uGB8zODMtNygtLisBCgoKDg0OGhAQGy0lHyUtLS0tLS0tLS0tLS0tLy0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSstLS0tLS0tLS0tLf/AABEIALcBEwMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAAEBQIDBgABB//EAEgQAAIBAgQDBQQHBQYDCAMAAAECEQADBBIhMQVBUQYTImFxMoGRoSNCUrHB0fAUcpLS4QczU2KCshVUc0Njg5OiwuLxFiRk/8QAGQEAAwEBAQAAAAAAAAAAAAAAAQIDAAQF/8QALBEAAgICAgECBQQCAwAAAAAAAAECERIhAzFBE2EiMlFxgUKhsdEEkSMzUv/aAAwDAQACEQMRAD8A" 
        alt="SampleImage" />
于 2017-12-06T11:42:14.620 回答