Link to home
Start Free TrialLog in
Avatar of shanemay
shanemayFlag for United States of America

asked on

How Upload an image from source URL to SQL Server

I am able to upload images to a database using a form, however, I need to upload images via a direct url such as http://www.domainname.com/image1.gif

I understand that I may have to use WebClient  and Stream, however, I am not able to make it work.  I am using C#, ASP.NET 3.5.

Thank you for any feedback or advice.  
Avatar of techExtreme
techExtreme
Flag of India image

Hello,
Use this code to get the array of bytes of the image.

static public byte[] GetBytesFromUrl(string url)
{
byte[] b;
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
WebResponse myResp = myReq.GetResponse();

Stream stream = myResp.GetResponseStream();
//int i;
using (BinaryReader br = new BinaryReader(stream))
{
//i = (int)(stream.Length);
b = br.ReadBytes(500000);
br.Close();
}
myResp.Close();
return b;
}

And use the following code to save it to disk.

static public void WriteBytesToFile(string fileName, byte[] content)
{
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
try
{
w.Write(content);
}
finally
{
fs.Close();
w.Close();
}

}

Source: http://bytes.com/forum/thread235707.html

Let me know if you find any issues, I'll explain as required.

Hope it helped!
ASKER CERTIFIED SOLUTION
Avatar of techExtreme
techExtreme
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shanemay

ASKER

Thank you for the help,  It works fine.  I really appreciate it.  
Thanks :)