惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
Schneier on Security
Schneier on Security
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
A
About on SuperTechFans
Recorded Future
Recorded Future
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
Hugging Face - Blog
Hugging Face - Blog
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
V2EX - 技术
V2EX - 技术
N
Netflix TechBlog - Medium
F
Fortinet All Blogs
V
Visual Studio Blog
Martin Fowler
Martin Fowler
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Exploit Database - CXSecurity.com
F
Full Disclosure
Scott Helme
Scott Helme
H
Heimdal Security Blog
博客园 - 叶小钗
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
Application and Cybersecurity Blog
Application and Cybersecurity Blog
V
Vulnerabilities – Threatpost
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
WordPress大学
WordPress大学
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Troy Hunt's Blog
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
Jina AI
Jina AI
S
Securelist
小众软件
小众软件
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
AWS News Blog
AWS News Blog
N
News and Events Feed by Topic
博客园 - 三生石上(FineUI控件)
量子位

博客园 - 名可真难起

CSOM创建Content Type并指定GUID SharePoint还原大体积内容数据库报空间错误的解决办法 SharePoint 2013: Knockout JS + Rest Service展示List数据 SharePoint 2013—创建Master Page 通过SPWeb.EnsureUser将SPUser添加到网站 如何在页面中使用sharepoint中的富文本编辑器控件 SharePoint 2010 Visual Web Part发布后找不到文件的错误 SharePoint 2010通过Web Service读取和修改Excel文件 SharePoint 2010中Metadata Service 解决SharePoint2010中Excel刷新出错的问题 SharePoint 2007搜索网站开启匿名访问 SharePoint 2010: 取得浏览最多的网站 在Windows 7中如何用另一个用户运行程序 不仅仅是土豆 SharePoint 2010运行环境 最近比较懒 如何在ActionResult中返回XML类型 - 名可真难起 - 博客园 SharePoint 2010 Development Stack SharePoint 2010 Visual Web Part Demo--展示网站中的List.
通过编程的方式给列表项添加附件
名可真难起 · 2011-02-24 · via 博客园 - 名可真难起

最近项目中有一个需求,给列表项添加附件。用列表来保存信息是非常方便的,但是在附加文件时遇到了一些周折。在此记录下所用的部分代码,欢迎和大家多多交流。

假设页面有一个上传控件,名叫“FileUpload1”,在点击上传之后,后台得到上传的文件,然后将文件附加到列表项中。

if (FileUpload1.HasFile)

{

                //以下代码得到文件流 

                    Stream fStream = FileUpload1.PostedFile.InputStream;

                    byte[] contents = new byte[fStream.Length];

                    fStream.Read(contents, 0, (int)fStream.Length);

                    fStream.Close();

                    fStream.Dispose();

FileInfo file = new FileInfo(FileUpload1.PostedFile.FileName);

SPListItem item = list.GetItemById(itemId);//要给该记录附加文件

if (item != null)

                    {

                        item.Attachments.Add(file.Name, contents);//给该记录附加文件,可以附加多个附件。

                        item.Update();

                    }

以上是给列表项附加一个文件,下面代码是直接给文档库上传一个文件。如果以资源管理器的方式打开一个文档库,可以看到,文档库中的文件形式和本地的文件夹类似,所以给文档库上传文件的上传方式和asp.net往服务器传文件几乎是一样的。

using (SPWeb site = SPContext.Current.Web)

           {

               try

               {

                   site.AllowUnsafeUpdates = true;

                   FileStream fStream = FileUpload1.PostedFile.InputStream;

                   byte[] contents = new byte[fStream.Length];

                   fStream.Read(contents, 0, (int)fStream.Length);

                   fStream.Close();

                   site.Files.Add(destUrl, contents, true);// destUrl是文件在服务器上存储的地址,例如http://mysite/document/abc.txt

                   site.AllowUnsafeUpdates = false;

               }

               catch

               { }

           }

致力于SharePoint开发. QQ:28748451.