Solved

How Upload an image from source URL to SQL Server

Posted on 2008-10-30
4
444 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you for the help,  It works fine.  I really appreciate it.  
0
 
LVL 12

Expert Comment

by:techExtreme
Comment Utility
Thanks :)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQl help with selection 14 43
Hide Tab Page 3 18
Showdialog 8 20
Get list of word ducuments in a folder 10 9
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

11 Experts available now in Live!

Get 1:1 Help Now