从远程URL获取映像并上载到SharePoint 2010

时间:2015-03-25 20:44:23

标签: upload 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");
                }
            }
        }

2 个答案:

答案 0 :(得分:1)

这就像一个魅力

        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
        }
    }

答案 1 :(得分:0)

尝试使用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