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.