从远程 URL 获取图像并上传到 SharePoint 2010
Get Image from remote URL and upload to SharePoint 2010
我有一些用于将图像上传到 SharePoint 的代码,它运行良好。我现在需要扩展这段代码并从远程获取图像 URL...图书馆。
感谢任何帮助。
当前代码:
string filePath = @"C:\Users\username\Desktop\logo.jpg";
string siteURL = "http://mydevsite.com/";
string libraryName = "SOI_Images";
using (SPSite oSite = new SPSite(siteURL))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
if (!System.IO.File.Exists(filePath))
throw new FileNotFoundException("File not found.", filePath);
SPFolder libFolder = oWeb.Folders[libraryName];
// Prepare to upload
string fileName = System.IO.Path.GetFileName(filePath);
FileStream fileStream = File.OpenRead(filePath);
//Check the existing File out if the Library Requires CheckOut
if (libFolder.RequiresCheckout)
{
try
{
SPFile fileOld = libFolder.Files[fileName];
fileOld.CheckOut();
}
catch { }
}
// Upload document
SPFile spfile = libFolder.Files.Add(fileName, fileStream, true);
// Commit
//myLibrary.Update();
//Check the File in and Publish a Major Version
if (libFolder.RequiresCheckout)
{
spfile.CheckIn("Upload Comment", SPCheckinType.MajorCheckIn);
spfile.Publish("Publish Comment");
}
}
}
尝试使用 HttpWebRequest 获取图像,然后保存生成的流。
这是关于该主题的另一个答案和一个简短的片段。
How do I programmatically save an image from a URL?
string imageUrl = @"http://www.somedomain.com/image.jpg";
using( HttpWebRequest imageRequest = (HttpWebRequest)WebRequest.Create(imageUrl))
using(WebResponse imageResponse = imageRequest.GetResponse())
using(Stream responseStream = imageResponse.GetResponseStream())
{
//save your file to the document library here.
}//close stream, webResponse and HttpWebRequest
这很有效
public static void MyUploadtoSharepoint(string _site, string _ListName, string _ImageURL, string _dateTaken, string _uid, string _userEmail)
{
try
{
SPSite site = new SPSite(_site);
SPWeb web = site.RootWeb;
// SPWeb web = SPContext.Current.Site.RootWeb;
web.AllowUnsafeUpdates = true;
SPList pics = web.Lists[_ListName];
//use WebRequest to create stream
WebRequest reqImg = WebRequest.Create(_ImageURL);
//reqImg.UseDefaultCredentials = true;
WebResponse imgResponse = reqImg.GetResponse();
StreamReader reader = new StreamReader(imgResponse.GetResponseStream());
Hashtable metaData = new Hashtable();
metaData.Add("UserName", _uid);
metaData.Add("DateTaken", _dateTaken);
metaData.Add("userEmail", _userEmail);
using (Image imgOriginal = Image.FromStream(reader.BaseStream, true))
{
MemoryStream mstream = new MemoryStream();
imgOriginal.Save(mstream, imgOriginal.RawFormat);
MemoryStream msNew = null;
using (msNew = new MemoryStream(mstream.GetBuffer(), 0, mstream.GetBuffer().Length))
{
msNew.Write(mstream.GetBuffer(), 0, mstream.GetBuffer().Length);
var files = pics.RootFolder.Files.Add(_uid + ".png", msNew.ToArray(), metaData, true);
files.Item.SystemUpdate(false);
// files.CheckIn("");
}
}
web.AllowUnsafeUpdates = false;
site.Dispose();
web.Dispose();
imgResponse.Close();
reader.Dispose();
}
catch (Exception)
{
//do nothing
}
}
我有一些用于将图像上传到 SharePoint 的代码,它运行良好。我现在需要扩展这段代码并从远程获取图像 URL...图书馆。
感谢任何帮助。
当前代码:
string filePath = @"C:\Users\username\Desktop\logo.jpg";
string siteURL = "http://mydevsite.com/";
string libraryName = "SOI_Images";
using (SPSite oSite = new SPSite(siteURL))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
if (!System.IO.File.Exists(filePath))
throw new FileNotFoundException("File not found.", filePath);
SPFolder libFolder = oWeb.Folders[libraryName];
// Prepare to upload
string fileName = System.IO.Path.GetFileName(filePath);
FileStream fileStream = File.OpenRead(filePath);
//Check the existing File out if the Library Requires CheckOut
if (libFolder.RequiresCheckout)
{
try
{
SPFile fileOld = libFolder.Files[fileName];
fileOld.CheckOut();
}
catch { }
}
// Upload document
SPFile spfile = libFolder.Files.Add(fileName, fileStream, true);
// Commit
//myLibrary.Update();
//Check the File in and Publish a Major Version
if (libFolder.RequiresCheckout)
{
spfile.CheckIn("Upload Comment", SPCheckinType.MajorCheckIn);
spfile.Publish("Publish Comment");
}
}
}
尝试使用 HttpWebRequest 获取图像,然后保存生成的流。 这是关于该主题的另一个答案和一个简短的片段。
How do I programmatically save an image from a URL?
string imageUrl = @"http://www.somedomain.com/image.jpg";
using( HttpWebRequest imageRequest = (HttpWebRequest)WebRequest.Create(imageUrl))
using(WebResponse imageResponse = imageRequest.GetResponse())
using(Stream responseStream = imageResponse.GetResponseStream())
{
//save your file to the document library here.
}//close stream, webResponse and HttpWebRequest
这很有效
public static void MyUploadtoSharepoint(string _site, string _ListName, string _ImageURL, string _dateTaken, string _uid, string _userEmail)
{
try
{
SPSite site = new SPSite(_site);
SPWeb web = site.RootWeb;
// SPWeb web = SPContext.Current.Site.RootWeb;
web.AllowUnsafeUpdates = true;
SPList pics = web.Lists[_ListName];
//use WebRequest to create stream
WebRequest reqImg = WebRequest.Create(_ImageURL);
//reqImg.UseDefaultCredentials = true;
WebResponse imgResponse = reqImg.GetResponse();
StreamReader reader = new StreamReader(imgResponse.GetResponseStream());
Hashtable metaData = new Hashtable();
metaData.Add("UserName", _uid);
metaData.Add("DateTaken", _dateTaken);
metaData.Add("userEmail", _userEmail);
using (Image imgOriginal = Image.FromStream(reader.BaseStream, true))
{
MemoryStream mstream = new MemoryStream();
imgOriginal.Save(mstream, imgOriginal.RawFormat);
MemoryStream msNew = null;
using (msNew = new MemoryStream(mstream.GetBuffer(), 0, mstream.GetBuffer().Length))
{
msNew.Write(mstream.GetBuffer(), 0, mstream.GetBuffer().Length);
var files = pics.RootFolder.Files.Add(_uid + ".png", msNew.ToArray(), metaData, true);
files.Item.SystemUpdate(false);
// files.CheckIn("");
}
}
web.AllowUnsafeUpdates = false;
site.Dispose();
web.Dispose();
imgResponse.Close();
reader.Dispose();
}
catch (Exception)
{
//do nothing
}
}