1

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.

According to this site: http://social.technet.microsoft.com/Forums/sharepoint/en-US/50deca94-71b4-4081-9dc6-81fc7e86886f/how-to-read-image-file-from-a-url-and-save-the-image-in-image-library-in-sharepoint-2010

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";
    spFileCollection.Add(targetFileUrl,imageBytes, true); 
}

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.

So I was able to download an image using this code:

string localFilename = @"c:\tofile.jpg";
        using (WebClient client = new WebClient())
        {
            client.DownloadFile("http://img.talkandroid.com/uploads/2013/03/wpid-photo-jan-14-2013-1115-am.jpg", localFilename);
        }

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.

3
  • What is your 'form'? a list item?
    – banana
    Commented Sep 11, 2013 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
    Commented Sep 11, 2013 at 14:21
  • Why not use Event Receiver instead of jquery?
    – banana
    Commented Sep 12, 2013 at 6:02

2 Answers 2

2

You can try the below code for file upload:

using (SPWeb web = SPContext.Current.Site.OpenWeb())
                        {
                            SPList docLibrary = web.Lists["Documents"];
                            web.AllowUnsafeUpdates = true;
                            Stream fStream = FileUpload1.PostedFile.InputStream;

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

                            web.AllowUnsafeUpdates = false;
                        }
0

You can use the code in the following URL to upload pictures to DocLib using jquery.

sharepoint 2010 upload file to document library using jquery

1
  • Please do not post link-only answers. Add the details from the link to your post with the link as source. This way, the answer will still be helpful if the link breaks Commented Feb 9, 2014 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.