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
Solved

How Upload an image from source URL to SQL Server

Posted on 2008-10-30
4
475 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

791 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