//Create Class
public class Common
{
public string ViewImage(byte[] arrayImage)
{
return "data:image/png;base64," + Convert.ToBase64String(arrayImage, 0, arrayImage.Length);
}
public string ViewVideo(byte[] arrayImage)
{
return "data:video/mp4;base64," + Convert.ToBase64String(arrayImage, 0, arrayImage.Length);
}
//****
public String ConvertImageURLToBase64(String url)
{
StringBuilder _sb = new StringBuilder();
Byte[] _byte = this.GetImage(url);
_sb.Append(Convert.ToBase64String(_byte, 0, _byte.Length));
return _sb.ToString();
}
public byte[] GetImage(string url)
{
Stream stream = null;
byte[] buf;
try
{
WebProxy myProxy = new WebProxy();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
stream = response.GetResponseStream();
using (BinaryReader br = new BinaryReader(stream))
{
int len = (int)(response.ContentLength);
buf = br.ReadBytes(len);
br.Close();
}
stream.Close();
response.Close();
}
catch (Exception exp)
{
buf = null;
}
return (buf);
}
}
//************************** html code *************
Show image by url
<img src="@cm.ViewImage( cm.GetImage("https://www.aswtech.in/Content/Home/images/slider/fraction/3.png"))" width="300px" height="200px" alt="Alternate Text" />
Show Image /Vidwo From Database
<video src='@cm.ViewVideo((byte[])dt.Rows[i]["XDATA"])' controls="true" width="400" height="350" loop="true"/>
<img src="@cm.ViewImage((byte[])dt.Rows[i]["XDATA"])" width="300px" height="200px" alt="Alternate Text" />
Database Table
CREATE TABLE [dbo].[PROMOTION](
[SRNO] [int] IDENTITY(1,1) NOT NULL,
[TDATE] [datetime] NULL,
[XDATA] [varbinary](max) NULL
)
Convert uploaded File to bytes array
public ActionResult AddPromotion(FormCollection frm, HttpPostedFileBase file1)
{
byte[] bytes = null;
if (file1 != null)
{
path = Path.GetFileName(file1.FileName);
using (BinaryReader br = new BinaryReader(file1.InputStream))
{
bytes = br.ReadBytes(file1.ContentLength);
}
}
//Save bytes to database
}