JAVASCRIPT - 保存帆布图像到数据
此代码在点击时将base64字符串绑定到隐藏字段。
save.addEventListener('click', function (event) {
var dataUrl = canvas.toDataURL();
$('txtbx').val(dataUrl);
});
VB.NET - 提交BASE64字符串SQL
此代码在单击时将base64字符串提交到数据库。 (作为VarChar
)
Protected Sub btn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn.Click
Dim base64 As String = hidUrl.Value
Dim base64Decode As String = base64.Replace("data:image/png;base64,", "")
Dim cmd As New SqlCommand("sp", con)
cmd.CommandType = CommandType.StoredProcedure
Dim image As New SqlParameter("@image", SqlDbType.VarChar, -1)
image .Direction = ParameterDirection.Input
cmd.Parameters.Add(image).Value = base64Decode
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
如何:
欢迎任何将图像字符串传递到varbinary字段的替代解决方案。
我已经阅读了很多关于此的文档但是无法实现一个工作示例。我现在需要一些专家指导。
简单解决方案
答案是简单地将字符串包装如下:
Convert.FromBase64String(base64Decode)
答案 0 :(得分:1)
要将base64字符串转换为二进制数据类型,请使用Convert.FromBase64String,然后使用字节数组将其作为二进制文件存储在数据库中。
我在一个表单中做了这个快速测试,它运行良好:
//got this datauri from http://corehtml5canvas.com/code-live/ch01/example-1.1/example.html
var datauri = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlgAAAEsCAYAAAAfPc2WAAAaqklEQVR4Xu3dS6LrNBYFUN4UqqZIj/HQY4owBapCKpWfbOvI2365V4sWcJ1jafkk2rET58cv/iFAgAABAgQIEIgK/IhWU4wAAQIECBAgQOAXAUsTECBAgAABAgTCAgJWGFQ5AgQIECBAgICApQcIECBAgAABAmEBASsMqhwBAgQIECBAQMDSAwQIECBAgACBsICAFQZVjgABAgQIECAgYOkBAgQIECBAgEBYQMAKgypHgAABAgQIEBCw9AABAgQIECBAICwgYIVBlSNAgAABAgQICFh6gAABAgQIECAQFhCwwqDKESBAgAABAgQELD1AgAABAgQIEAgLCFhhUOUIECBAgAABAgKWHiBAgAABAgQIhAUErDCocgQIECBAgAABAUsPECBAgAABAgTCAgJWGFQ5AgQIECBAgICApQcIECBAgAABAmEBASsMqhwBAgQIECBAQMDSAwQIECBAgACBsICAFQZVjgABAgQIECAgYOkBAgQIECBAgEBYQMAKgypHgAABAgQIEBCw9AABAgQIECBAICwgYIVBlSNAgAABAgQICFh6gAABAgQIECAQFhCwwqDKESBAgAABAgQELD1AgAABAgQIEAgLCFhhUOUIECBAgAABAgKWHiBAgAABAgQIhAUErDCocgQIECBAgAABAUsPECBAgAABAgTCAgJWGFQ5AgQIECBAgICApQcIECBAgAABAmEBASsMqhwBAgQIECBAQMDSAwQIECBAgACBsICAFQZVjgABAgQIECAgYOkBAgQIECBAgEBYQMAKgypHgAABAgQIEBCw9AABAgQIECBAICwgYIVBlSNAgAABAgQICFh6gAABAgQIECAQFhCwwqDKESBAgAABAgQELD1AgAABAgQIEAgLCFhhUOUIECBAgAABAgKWHiBAgAABAgQIhAUErDCocgQIECBAgAABAUsPECBAgAABAgTCAgJWGFQ5AgQIECBAgICApQcIECBAgAABAmEBASsMqhwBAgQIECBAQMDSAwQIECBAgACBsICAFQZVjgABAgQIECAgYOkBAgQIECBAgEBYQMAKgypHgAABAgQIEBCw9AABAgQIECBAICwgYIVBlSNAgAABAgQICFh6gAABAgQIECAQFhCwwqDKESBAgAABAgQELD1AgAABAgQIEAgLCFhhUOUIECBAgAABAgKWHiBAgAABAgQIhAUErDCocgQIECBAgAABAUsPECBAgAABAgTCAgJWGFQ5AgQIECBAgICApQcIECBAgAABAmEBASsMqhwBAgQIECBAQMDSAwQIECBAgACBsICAFQZVjgABAgQIECAgYOkBAgQIECBAgEBYQMAKgypHgAABAgQIEBCw9AABAgQIECBAICwgYIVBlSNAgAABAgQICFh6gAABAgQIECAQFhCwwqDKESBAgAABAgQELD1AgAABAgQIEAgLCFhhUOUIECBAgAABAgKWHiBAgAABAgQIhAUErDCocgQIECBAgAABAUsPECBAgAABAgTCAgJWGFQ5AgQIECBAgICApQcIECBAgAABAmEBASsMqhwBAgQIECBAQMDSAwQIECBAgACBsICAFQZVjgABAgQIECAgYOkBAgQIECBAgEBYQMAKgypHgAABAgQIEBCw9AABAgQIECBAICwgYIVBlSNAgAABAgQICFh6gAABAgQIECAQFhCwwqDKESBAgAABAgQELD1AgAABAgQIEAgLCFhhUOUIECBAgAABAgKWHiBAgAABAgQIhAUErDCocgQIECBAgAABAUsPECBAgAABAgTCAgJWGFQ5AgQIECBAgICApQcIECBAgAABAmEBASsMqhwBAgQIECBAQMDSAwQIECBAgACBsICAFQZVjgABAgQIECAgYOkBAgQIECBAgEBYQMAKgypHgAABAgQIEBCw9AABAgQIECBAICwgYIVBlSNAgAABAgQICFh6gAABAgQIECAQFhCwwqDKESBAgAABAgQELD1AgAABAgQIEAgLCFhhUOUIECBAgAABAgKWHiBAgAABAgQIhAUErDCocgQIECBAgAABAUsPECBAgAABAgTCAgJWGFQ5AgQIECBAgICApQcIECBAgAABAmEBASsMqhwBAgQIECBAQMDSAwQIECBAgACBsICAFQZVjgABAgQIECAgYOkBAgQIECBAgEBYQMAKgypHgAABAgQIEBCw9AABAgQIECBAICwgYIVBlSNAgAABAgQICFh6gAABAgQIECAQFhCwwqDKESBAgAABAgQELD1AgAABAgQIEAgLCFhhUOUIEMgI/Pr7n3+vVfrjt397/cpQq0KAwAECXqAOQFWyJfB3Y7H8Eeq/Vu3LGFL19x7R6viOtNo7l2MffwlVf/z2r9JOfv39r1+ErRKZjQkQOEEgtMCdMFK7+OICf/99WQhv/1wX0VQAeq592Ue2/l766viOtNo7lyMffw+Wj72yfhbrHsYErSOPjdoECFQFBKyqmO0HBY4MDdUAMziF4YdVx3ek1fAkDnzgNVj1hqqlgdzPfKWC+4FTVpoAgW8vIGB9+0P8KRM8MjRUA8zZJtXxHWl19ty39vdu8/iIpUt/S5/PErK2vP2dAIGzBASss6Sn38+RoaEaYM4+GNXxHWl19tzX9rccrq7Baumza7eaP360gpaQ9UnH2FgIzCsgYM177E+e+ZGhoRpgTp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4AAAQIECMQFBKw4qYIECBAgQIDA7AIC1uwdYP4ECBAgQIBAXEDAipMqSIAAAQIECMwuIGDN3gHmT4AAAQIECMQFBKw4qYIECBAgQIDA7AIC1uwdYP4ECBAgQIBAXEDAipMqSIAAAQIECMwuIGDN3gHmT4AAAQIECMQFBKw4qYIECBAgQIDA7AIC1uwdYP4ECBAgQIBAXEDAipMqSIAAAQIECMwuIGDN3gHmT4AAAQIECMQFBKw4qYIECBAgQIDA7AIC1uwdYP4ECBAgQIBAXEDAipMqSIAAAQIECMwuIGDN3gHmT4AAAQIECMQFBKw4qYIECBAgQIDA7AIC1uwdYP4ECBAgQIBAXEDAipMqSIAAAQIECMwuIGDN3gHmT4AAAQIECMQFBKw4qYIECBAgQIDA7AIC1uwdYP4ECBAgQIBAXEDAipMqSIAAAQIECMwuIGDN3gHmT4AAAQIECMQFBKw4qYIECBAgQIDA7AIC1uwdYP4ECBAgQIBAXEDAipMqSIAAAQIECMwuIGDN3gHmT4AAAQIECMQFBKw4qYIECBAgQIDA7AIC1uwdYP4ECBAgQIBAXEDAipMqSIAAAQIECMwuIGDN3gHmT4AAAQIECMQFBKw4qYIECBAgQIDA7AIC1uwdYP4ECBAgQIBAXEDAipMqSIAAAQIECMwuIGDN3gHmT4AAAQIECMQFBKw4qYIECBAgQIDA7AIC1uwdYP4ECBAgQIBAXEDAipMqSIAAAQIECMwuIGDN3gHmT4AAAQIECMQFBKw4qYIECBAgQIDA7AIC1uwdYP4ECBAgQIBAXEDAipMqSIAAAQIECMwuIGDN3gHmT4AAAQIECMQFBKw4qYIECBAgQIDA7AIC1uwdYP4ECBAgQIBAXEDAipMqSIAAAQIECMwuIGDN3gHmT4AAAQIECMQFBKw4qYIECBAgQIDA7AIC1uwdYP4ECBAgQIBAXEDAipMqSIAAAQIECMwu8B+03ZHSzNSl5QAAAABJRU5ErkJggg==";
var base64 = datauri.Substring(datauri.IndexOf(',') + 1);
var binary = Convert.FromBase64String(base64);
pictureBox1.Image = Image.FromStream(new MemoryStream(binary));
请记住更改SP参数以使用varbinary。
您的解码功能过于复杂,您不需要,只需更改参数创建:
Dim image As New SqlParameter("@image", SqlDbType.VarBinary, -1)
image .Direction = ParameterDirection.Input
cmd.Parameters.Add(image ).Value = Convert.FromBase64String(base64.Substring(base64.IndexOf(',') + 1););
您不需要任何char数组或流,只需删除URI(substring
段的无用部分,并将其转换为字节数组(Convert...
)。
ADO.NET会自动将字节数组转换为MSSQL预期的varbinary。
答案 1 :(得分:1)
也许您需要先将表格列[Image]更新为varbinary(max)。
然后,按以下步骤更新程序:
@image VARCHAR(MAX)
AS
BEGIN
SET NOCOUNT ON;
UPDATE [tbl]
SET [Image] = cast('' as xml).value('xs:base64Binary(sql:variable("@image"))', 'varbinary(max)')
END
尝试并告知我们是否有效。
编辑:
看起来您需要解码数据。我已经更新了proc。有关编码/解码的更多信息,请查看http://blog.falafel.com/t-sql-easy-base64-encoding-and-decoding/。