0

I have created empty SharePoint project in Visual Studio and have added SharePoint images mapped folder. Now I want to upload images to this folder which I get from Instagram. I have googled but have found code for uploading image to document library like below

 String fileToUpload = @"C:\YourFile.txt";
 String sharePointSite = "http://yoursite.com/sites/Research/";
 String documentLibraryName = "Shared Documents";

 using (SPSite oSite = new SPSite(sharePointSite))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
    if (!System.IO.File.Exists(fileToUpload))
        throw new FileNotFoundException("File not found.", fileToUpload);                    

    SPFolder myLibrary = oWeb.Folders[documentLibraryName];

    // Prepare to upload
    Boolean replaceExistingFiles = true;
    String fileName = System.IO.Path.GetFileName(fileToUpload);
    FileStream fileStream = File.OpenRead(fileToUpload);

    // Upload document
    SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);

    // Commit 
    myLibrary.Update();
}
}

I wish to upload in my SharePoint mapped images folder. How to do it?

Update1

Ok now i want to upload in document library because its seems like we cannot upload to hive directly. so i have below code which downloads images from instagram. My question is then how can i use this image i get to upload in document library.

do
        {
            WebRequest webRequest = null;
            if (webRequest == null && string.IsNullOrEmpty(nextPageUrl))
                webRequest = HttpWebRequest.Create(String.Format("https://api.instagram.com/v1/tags/{0}/media/recent?access_token={1}", strtagName, strAccessToken));
            else
                webRequest = HttpWebRequest.Create(nextPageUrl);

            var responseStream = webRequest.GetResponse().GetResponseStream();
            Encoding encode = System.Text.Encoding.Default;


            using (StreamReader reader = new StreamReader(responseStream, encode))
            {
                JToken token = JObject.Parse(reader.ReadToEnd());
                var pagination = token.SelectToken("pagination");

                if (pagination != null && pagination.SelectToken("next_url") != null)
                {
                    nextPageUrl = pagination.SelectToken("next_url").ToString();
                }
                else
                {
                    nextPageUrl = null;
                }

                var images = token.SelectToken("data").ToArray();

                foreach (var image in images)
                {
                    imageUrl = image.SelectToken("images").SelectToken("standard_resolution").SelectToken("url").ToString();

                    if (String.IsNullOrEmpty(imageUrl))
                        Console.WriteLine("broken image URL");

                    var imageResponse = HttpWebRequest.Create(imageUrl).GetResponse().GetResponseStream();

                    var imageId = image.SelectToken("id");
                    object outputDir = null;

                    using (var imageWriter = new StreamWriter(String.Format("{0}\\{1}.jpg", outputDir, imageId)))
                    {
                        imageResponse.CopyTo(imageWriter.BaseStream);
                        imageResponse.Flush();
                        Console.WriteLine("copied {0}", imageId);

                    }

                    return imageUrl;

                }


            }

        }
        while (!String.IsNullOrEmpty(nextPageUrl));
  • _images is a special folder, it's content is stored in hdd(ssd) rather then in database like site folders. To work with it, SharePoint has internal deployment API. If you really want to do this manually, you will have to write to each physical front end server of the farm. Take into consideration permissions needed. My opinion is that you gain no advantages over web folders/file libraries on that. – dbardakov Apr 23 '15 at 11:59
  • @dbardakov please see my updated question – Happy Apr 23 '15 at 12:17
0

This not the recommended way because

  1. File System data are normally not backed up
  2. If system upgrade/migration will loose files
  3. Files will not get propagated to other servers on the farm - This will be a major problem in case of multi server farm

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        string imagesPath = Path.Combine(SPUtility.GetGenericSetupPath("TEMPLATE"), @"LAYOUTS\Images");
    
        //Here you can use Normal File Operations to Path imagesPath
        using (System.IO.FileStream fs = System.IO.File.Create(imagesPath + FILENAME.EXTENSION))
        {
            //fs.WriteByte(i);          
        }
    });
    
  • Have you checked my updated question? – Happy Apr 23 '15 at 12:28
  • I am not able to understand what do i write in fileToUpload variable in my first part of code. In my second part of code i get images but really dont know how to save it? – Happy Apr 23 '15 at 12:31
  • Please please help me. I am still struggling on how to use the stream to save image to document library – Happy Apr 24 '15 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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