现在的位置: 首页 > 综合 > 正文

上传文件至sharepoint

2012年10月17日 ⁄ 综合 ⁄ 共 5209字 ⁄ 字号 评论关闭

 众所周知绝大多数OA办公软件都离不开他们的金牌合作伙伴à文件管理与上传!假如失去这位挚友,那它的功能也就大打折扣咯,更别说被企业得到友好,广泛的使用!而被大家熟悉的sharepoint也不例外哦!它的上传功能照样是Strong!本章节,笔者主要围绕文档权限,文档上传,角色分配3大模块来讲,并以FileUploadSolution进行演示和注释。

前提准备:

1.了解,熟悉sharepoint的权限架构,针对这块如果大家还不大了解的话,可以参考SDK2.了解sharepoint的文档上传,注意点有哪些,和直接在平时做的ASP.NET做的文档上传差异点在哪(这边要清楚,MOSS上很多路径是映射到IIS上面的,所以有些上传的地方就不能依旧以前的习惯直接给他一条路径,上传的时候,文件最好以filestream 的形式读取,写入,MOSS上传文件的最大值为2G

3.了解如何进行权限分配,以及如何提高普通用户的访问权限。带着这些疑问,或者更多的疑问一起更我这个初学者踏入UploadFile to sharepoint吧!

4.页面部署了QuikPart,以及如何用QuikPart包装用户控件(这块应该是最基本了,所以笔者也不再像刚开始那样一步步的叙述和截屏了,如有不明白前面篇章都有提到!)

 

首先。还是大家所熟悉的创建一个webapplication项目取名为FileUploadOnMosSolution.

1.在页面上添加一个用户控件页面命名为:FileUploadOnMoss.ascx,删除原先default.aspx页面。在该页面上添加一个UploadFile控件,两个下拉框,一个lable

 

2.记得添加sharepoint.dll程序集。

3.在项目属性生成后事件,直接写命令行以便生成项目时直接把程序集和用户控件界面拷贝到相应的目录进行部署,提高开发的效率(记得把80给成自己机子的端口):

copy "$(TargetDir)*.dll" C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin
copy "$(ProjectDir)*.ascx" C:\Inetpub\wwwroot\wss\VirtualDirectories\80\wpresources\FileUpload

 

项目源码:(笔者在这提醒:当你开发用户控件时,一定记得去源码页,点击文件->高级保存选项->编码模式:UTF-8,避免生成的webpart产生乱码)

  源码:

 

 1<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="FileUploadOnMoss.ascx.cs" Inherits="FileUploadSolution.FileUploadOnMoss" %>
 2<table cellpadding="0" cellspacing="0" style=" border:1px solid; width:250px">
 3    <tr>
 4        <td style="width:137px; height: 22px;">
 5            请选择列表:</td>
 6        <td style="border-left:1px solid; height: 22px;">
 7            <asp:DropDownList ID="ListTitleNameDrp" runat="server" OnSelectedIndexChanged="ListTitleNameDrp_SelectedIndexChanged"
 8                AutoPostBack="True" Width="105px">
 9            </asp:DropDownList></td>
10    </tr>
11    <tr>
12        <td style="width:137px;border-top:1px solid;">
13            请选择文件夹:</td>
14        <td style="border-top:1px solid; border-left:1px solid">
15            <asp:DropDownList ID="FolderNameDrp" runat="server" AutoPostBack="True" Width="104px">
16            </asp:DropDownList></td>
17    </tr>
18    <tr style="border-top:1px solid;">
19        <td colspan="2" style="height: 24px;">
20            <asp:FileUpload ID="FileUpload1" runat="server" Width="240px" /></td>
21    </tr>
22   <tr style="border-top:1px solid;">
23       <td colspan="2">
24            <asp:Button ID="UploadButton" runat="server" OnClick="UploadButton_Click" Text="Upload file" /><asp:Label ID="UploadStatusLabel" runat="server" ForeColor="Red"></asp:Label>
25       </td>
26    </tr>
27</table>
28<hr />
29

代码:

 

 

 

  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Collections;
  5using System.Web;
  6using System.Web.Security;
  7using System.Web.UI;
  8using System.Web.UI.WebControls;
  9using System.Web.UI.WebControls.WebParts;
 10using System.Web.UI.HtmlControls;
 11using Microsoft.SharePoint;
 12using System.IO;
 13
 14namespace FileUploadSolution
 15{
 16    public partial class FileUploadOnMoss : System.Web.UI.UserControl
 17    {
 18        protected void Page_Load(object sender, EventArgs e)
 19        {
 20            if(!IsPostBack)
 21              {
 22                BindListTitle();
 23             }

 24        }

 25        //上传按钮事件
 26        protected void UploadButton_Click(object sender, EventArgs e)
 27        {
 28            Boolean fileOK = false;
 29            string fileurl = FileUpload1.PostedFile.FileName;
 30            string filename=FileUpload1.FileName;
 31            SPWeb web = SPContext.Current.Web;
 32            string pathToCheckUrl = string.Empty;
 33            string pathToCheck = string.Empty;
 34            if (FolderNameDrp.SelectedValue!="")
 35            {
 36                pathToCheckUrl= web.Lists[ListTitleNameDrp.SelectedValue].RootFolder.SubFolders[FolderNameDrp.SelectedValue].Url;
 37            }

 38            else
 39            {
 40                pathToCheckUrl = web.Lists[ListTitleNameDrp.SelectedValue].RootFolder.Url;
 41            }

 42            pathToCheck = pathToCheckUrl +'/'+ filename;//获取文件所在的路径
 43           
 44            if (FileUpload1.HasFile)//判断上传文件是否加载了文件源
 45            {
 46                String fileExtension =System.IO.Path.GetExtension(filename).ToLower();
 47                //获取文件后缀名名与自行规定的文件类型进行匹配
 48                String[] allowedExtensions = 
 49                ".doc"".docx"".pptx"".xlsx"".ppt" };
 50                for (int i = 0; i < allowedExtensions.Length; i++)
 51                {
 52                    if (fileExtension == allowedExtensions[i])
 53                    {
 54                        fileOK = true;
 55                    }

 56                }

 57            }

 58            if (fileOK)
 59            {
 60                try
 61                {
 62                    if (System.IO.File.Exists(pathToCheck))//检索同目录下是否已存在了相同的文件名
 63                    {
 64                        //throw new ArgumentException(String.Format("{0} 该文件名已经存在!", filename), "filename");
 65                    }

 66                    else
 67                    {
 68                        FileUpload(fileurl, filename);
 69                        UploadStatusLabel.Text = "上传成功!";
 70                                            
 71                    }

 72                }

 73                catch
 74                {    //当同个目录下存在同名文件时,做出错误的提示!
 75                    UploadStatusLabel.Text 

抱歉!评论已关闭.