Solved

How Upload an image from source URL to SQL Server

Posted on 2008-10-30
4
481 Views
Last Modified: 2013-12-17
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.  
0
Comment
Question by:shanemay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 12

Expert Comment

by:techExtreme
ID: 22842218
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!
0
 
LVL 12

Accepted Solution

by:
techExtreme earned 500 total points
ID: 22842262
Also, forgot to mention about the sql part:

It is in C# too and you won't have problem integrating them.
TO put the image in sql database, just add one more function to what I already have mentioned to you.

Once you have the bytes array of the image, just use it as shown in this tutorial in the 'OnUpload' function.
http://www.codeproject.com/KB/web-image/PicManager.aspx

I'm not re-writing it here as it is explained very well there. Just go thru once.

Hope it helped!
0
 

Author Closing Comment

by:shanemay
ID: 31511691
Thank you for the help,  It works fine.  I really appreciate it.  
0
 
LVL 12

Expert Comment

by:techExtreme
ID: 22851345
Thanks :)
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question