上傳附加檔到清單附件(Code)

上傳附加檔到清單附件(Code)

    /// =============================================================
    /// 上傳附加檔
    /// =============================================================
    ///

    SPSite oSite = new SPSite("http://lab:27094/Docs/");
    SPWeb web = oSite.OpenWeb();

    SPList list = web.Lists["FileAttachment"];

    web.AllowUnsafeUpdates = true;
    SPListItem item = list.Items.Add();

    item["Title"] = "FileAttachment";

    DirectoryInfo d = new DirectoryInfo(@"c:\temp\");
    FileInfo[] files = d.GetFiles();
    foreach (FileInfo f in files)
    {

          FileStream fs = new FileStream(f.FullName, FileMode.Open, FileAccess.Read);
          BinaryReader br = new BinaryReader(fs);
          Byte[] ByteFile = br.ReadBytes((int)f.Length);

          SPAttachmentCollection oA = item.Attachments;
          oA.Add(f.FullName, ByteFile);

    }
    item.Update();
    web.AllowUnsafeUpdates = false;

    /// End

}