Solved

How Upload an image from source URL to SQL Server

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
A short film showing how OnPage and Connectwise integration works.
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 …

948 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now