码迷,mamicode.com
首页 > 其他好文 > 详细

えええ

时间:2015-10-02 16:00:25      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace _20150928
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine(HttpUtility.HtmlDecode("<div class="ExternalClass261ED404956E49D289332C8D5DE50427"><p><a href="/sites/00/0001/SiteAssets/Lists/Dialogue/AllItems/TCALProjectList.xlsx"><img width="16" height="16" class="ms-asset-icon ms-rtePosition-4" src="/_layouts/15/images/icxlsx.png" alt="" />TCALProjectList.xlsx</a>?</p></div>"));
            ////string str = "http://baidu.com.cn/hello%20I%27am a apple/te%27st.txt";
            ////Console.WriteLine(str.Length);
            //Console.ReadKey();

            string mainSite = ConfigurationManager.AppSettings["SiteUrl"];
            string siteUrl = mainSite + "sites/" + "te" + "/" + "test" + "/";
            string listName = "test";
            string folderServerRelativeUrl = "/test/test/";

            SPFolder folder = CreateFolderInList(siteUrl, listName, folderServerRelativeUrl);
            folder = GetSubFolderNewSystem(folder, "test");


            SPFolder folderNew = folder;
            string strURL = "";//web service get data
            string fileName = "";
            Stream stream = DownLoadAttachment(strURL);
            if (stream != null)
            {
                MigrationFromFileToFile(folderNew, fileName, stream);
            }

            string bodystring = "<a href=" + folderNew.Url.Remove(0, mainSite.Length) + fileName + " ><img src=" + "/_layouts/15/images/icxlsx.png" + " />" + fileName + "</a>";

        }

        public static SPFolder CreateFolderInList(string siteUrl, string listName, string folderServerRelativeUrl)
        {
            SPFolder returnFolder = null;
            using (SPSite site = new SPSite(siteUrl))
            using (SPWeb web = site.OpenWeb())
            {
                SPList list = web.Lists[listName];
                SPFolder folder = list.RootFolder;
                string[] folderNames = folderServerRelativeUrl.Split(/);
                foreach (string folderName in folderNames)
                {
                    if (!string.IsNullOrEmpty(folderName))
                    {
                        bool exitFolder = false;
                        if (folder.SubFolders.Count > 0)
                        {
                            foreach (SPFolder subFolder in folder.SubFolders)
                            {
                                if (subFolder.Name.Equals(folderName))
                                {
                                    exitFolder = true;
                                    folder = subFolder;
                                    break;
                                }
                            }
                        }
                        if (!exitFolder)
                        {
                            //フォルダー作成
                            folder.SubFolders.Add(folderName);
                            folder = folder.SubFolders[folderName];
                        }
                    }

                }
                returnFolder = folder;
            }
            return returnFolder;
        }


        public static SPFolder GetSubFolderNewSystem(SPFolder parentFolder, string folderName)
        {
            SPFolder subFolder = null;
            try
            {
                subFolder = parentFolder.SubFolders[folderName];
                if (!subFolder.Exists)
                {
                    parentFolder.SubFolders.Add(folderName);
                    subFolder = parentFolder.SubFolders[folderName];
                }
            }
            catch
            {
                parentFolder.SubFolders.Add(folderName);
                subFolder = parentFolder.SubFolders[folderName];
            }
            return subFolder;
        }


        public static Stream DownLoadAttachment(string strURL)
        {
            Stream stream;
            try
            {
                WebClient myWebClient = new WebClient();
                myWebClient.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["OldUserName"], ConfigurationManager.AppSettings["OldUserPassword"], ConfigurationManager.AppSettings["OldDomain"]);
                byte[] pagedata = myWebClient.DownloadData(strURL);
                stream = new MemoryStream(pagedata);
                myWebClient.Dispose();
                return stream;
            }
            catch (Exception ex)
            {
                return null;
            }

        }


        public static void MigrationFromFileToFile(SPFolder folderNew, string fileName, Stream stream)
        {
            SPFile subFileNew = null;
            byte[] contents = StreamToBytes(stream);
            if (contents != null)
            {
                subFileNew = folderNew.Files.Add(folderNew.Url + "/" + fileName, contents, true);
            }
        }

        public static byte[] StreamToBytes(System.IO.Stream stream)
        {
            int maxsize = Int32.Parse(ConfigurationManager.AppSettings["maxSize"].ToString());
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                byte[] bytes = new byte[maxsize];
                int read;
                while ((read = stream.Read(bytes, 0, bytes.Length)) > 0)
                {
                    ms.Write(bytes, 0, read);
                    if (ms.ToArray().Length > maxsize)
                    {
                        return null;
                    }
                }
                return ms.ToArray();
            }

        }


    }

}

 

えええ

标签:

原文地址:http://www.cnblogs.com/wodegui/p/4852195.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!