shanemay
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for the help, It works fine. I really appreciate it.
Thanks :)
Use this code to get the array of bytes of the image.
static public byte[] GetBytesFromUrl(string url)
{
byte[] b;
HttpWebRequest myReq = (HttpWebRequest)WebRequest
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!