Answered by:
Pull Image From Directory and Save To Database as Image Data type

Question
-
User727214845 posted
Hello,
Please i have images in Directory but i want to pull them and save to my database as Image Data type .
BEGIN TRANSACTION SET QUOTED_IDENTIFIER ON SET ARITHABORT ON SET NUMERIC_ROUNDABORT OFF SET CONCAT_NULL_YIELDS_NULL ON SET ANSI_NULLS ON SET ANSI_PADDING ON SET ANSI_WARNINGS ON COMMIT BEGIN TRANSACTION GO CREATE TABLE dbo.Table_1 ( Photos image NULL, id int NOT NULL IDENTITY (1, 1) ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO ALTER TABLE dbo.Table_1 SET (LOCK_ESCALATION = TABLE) GO COMMIT
Dim ImageURl As String = "http://localhost/api.sscoe.edu.ng/_photo/7a5942ca-a9f0-49b3-9e34-a9185c631bee.png" Dim ImageData As Byte() = Convert.FromBase64String(ImageURl.Replace("data:image/png;base64,", ""))
That is the sample code i have used, when i try saving to database i got an error
he 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.
Thank you
Sunday, May 30, 2021 4:46 PM
Answers
-
User727214845 posted
Hello,
I solve this issue by using a function that will do the conversion and used the converted image to save to my database, please find below the conversion Function.
Public Function GetImg(ByVal url As String) As Byte() Dim stream As Stream = Nothing Dim buf As Byte() Try Dim myProxy As WebProxy = New WebProxy() Dim req As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest) Dim response As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse) stream = response.GetResponseStream() Using br As BinaryReader = New BinaryReader(stream) Dim len As Integer = CInt((response.ContentLength)) buf = br.ReadBytes(len) br.Close() End Using stream.Close() response.Close() Catch exp As Exception buf = Nothing End Try Return (buf) End Function
and i call the function:
Dim Image As Byte() = x.GetImg("http://localhost/api.sscoe.edu.ng/_photo/7a5942ca-a9f0-49b3-9e34-a9185c631bee.png")
- Marked as answer by An0nym0u5User Tuesday, September 21, 2021 12:00 AM
Monday, May 31, 2021 3:52 PM
All replies
-
User-1545767719 posted
by using javascript, obtain a data url string and upload to web service (or equivalent) which can receive the uploaded data url string and convert it to byte array as you do. you will have to add canvas in the aspx page and draw the image to canvas to obtain data url.
Sunday, May 30, 2021 11:53 PM -
User1535942433 posted
Hi hydar,
There are two issues for your problem.
1.Your string is not a multiple of 4 long. It needs to be padded to a multiple of 4 using '=' characters.
2.It looks like it's the format of base 64 used for URLs and suchlike, "modified Base64 for URL". This uses - instead of + and _ instead of /.
More details,you could refer to below article:
Best regards,
Yijing Sun
Monday, May 31, 2021 2:24 AM -
User727214845 posted
Hello,
I solve this issue by using a function that will do the conversion and used the converted image to save to my database, please find below the conversion Function.
Public Function GetImg(ByVal url As String) As Byte() Dim stream As Stream = Nothing Dim buf As Byte() Try Dim myProxy As WebProxy = New WebProxy() Dim req As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest) Dim response As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse) stream = response.GetResponseStream() Using br As BinaryReader = New BinaryReader(stream) Dim len As Integer = CInt((response.ContentLength)) buf = br.ReadBytes(len) br.Close() End Using stream.Close() response.Close() Catch exp As Exception buf = Nothing End Try Return (buf) End Function
and i call the function:
Dim Image As Byte() = x.GetImg("http://localhost/api.sscoe.edu.ng/_photo/7a5942ca-a9f0-49b3-9e34-a9185c631bee.png")
- Marked as answer by An0nym0u5User Tuesday, September 21, 2021 12:00 AM
Monday, May 31, 2021 3:52 PM