How to read image file from a URL and save the image in image library in SharePoint 2010

I have a form, and that form has a field for a Picture and a Thumbnail which are currently Single Text fields. What I would like to do is when people input a URL on those fields, I would like to get the image of that URL and save it onto a SharePoint Picture Library.

I can find the value of the fields by using jquery, but I am wondering how can I find them on the back end.

It looks like I need this to get the image in the SharePoint Library

using (SPSite oSiteCollection = new SPSite("http://localhost"))
{
SPFolder oFolder = oSiteCollection.AllWebs["TargetWebSite"].GetFolder("TargetDocumentLibrary");
SPFileCollection spFileCollection = oFolder.Files;
string targetFileUrl = spFileCollection.Folder.Url + "/" + "cust.png";
}


My issue is how do I get the url, is it okay if I just use jquery, or how do I tell this I have the url so it can save it to the appropriate folder.

string localFilename = @"c:\tofile.jpg";
using (WebClient client = new WebClient())
{
}


but how would I go about it and

1. Save it to a sharepoint picture library, something like this: SPList mylist=objWeb.Lists["Shared"];.

2. I will need to use ajax to pass my parameter? such as instead of a hardcode url, a url that somebody inputs in a field.

Thanks.

• What is your 'form'? a list item? – banana Sep 11 '13 at 7:21
• Yes it is a list item. And I want to modify the uploading an image. I want to be able to put any url with an image..ex blog.heartland.org/wp-content/uploads/2013/07/Google.jpg and when I either put the link on the input field or when I submit it, it will grab the image and save it into my Picture Library. Does it make sense? – Diana Sep 11 '13 at 14:21
• Why not use Event Receiver instead of jquery? – banana Sep 12 '13 at 6:02

You can try the below code for file upload:

using (SPWeb web = SPContext.Current.Site.OpenWeb())
{
SPList docLibrary = web.Lists["Documents"];

SPFile file = web.Files.Add(web.Url.ToString() + "/" + docLibrary.Title.ToString() + "/" + FileUpload1.FileName, fStream, true);
file.Update();
docLibrary.Update();