从本地硬盘中上传文件到sps站点指定位置

       注:我们所建的项目必须在站点所在的website下面的virtual directory, 在虚拟服务器的managed path里面把这个虚拟路径excluded.

 从本地硬盘中上传文件到sps站点

页面上有一个HtmlInputFile, Button, TextBox, Microsoft.SharePoint.WebControls.FormDigest,

private void Button1_Click(object sender, System.EventArgs e)

      {

            if(this.File1.PostedFile==null)

            {

                  return;

            }

            string destUrl=this.TextBox1.Text;//http://is-hezhou:81/check  这是上传文件的存储位置,http://is-hezhou:81 是站点名,check是文件名

            SPWeb site=new SPSite(destUrl).OpenWeb();

            Stream fstream=this.File1.PostedFile.InputStream;

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

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

            fstream.Close();

            EnsureParentFolder(site,destUrl);

            site.Files.Add(destUrl,contents);

      }

 

      private string EnsureParentFolder(SPWeb parentsite,string desturl)

      {

            desturl=parentsite.GetFile(desturl).Url;

            int index=desturl.LastIndexOf("/");

            string parentfolderurl=string .Empty;

            if(index>-1)

            {

                  parentfolderurl=desturl.Substring(0,index);

                  SPFolder parentfolder=parentsite.GetFolder(parentfolderurl);

                  if(!parentfolder.Exists)

                  {

                       SPFolder currentfolder=parentsite.RootFolder;

                      

                       foreach(string folder in parentfolderurl.Split('/'))

                       {

                             currentfolder=currentfolder.SubFolders.Add(folder);

                       }

                  }

            }

            return parentfolderurl;

      }

上传上去之后,我们可以通过http://is-hezhou:81/check 这个地址去访问文档。但这篇文档不属于任何一个文档库,或者列表,从站点的内容结构层次中我们是发现不了这篇文档的,它属于这个站点,但通过搜索我们可以发现。

2.  从本地硬盘中上传文件到sps站点文档库中

3.  利用控件实现把本地多个文档上传到sps站点中指定的文档库里面。

添加formdigest控件属于名称空间 Microsoft.Sharepoint.WebControl

<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

添加实现都文档上传的控件

<OBJECT id=iduploadctl height=500 width="100%" classid="CLSID:07B06095-5687-4D13-9E32-12B4259C9813</OBJECT" name=iduploadctl VIEWASTEXT>

<PARAM NAME="_cx" VALUE="19262">

<PARAM NAME="_cy" VALUE="13229">

</OBJECT>

<head></head>之间实现javascript方法

<script language=javascript>

               function UploadFiles()

               {

                      document.all.iduploadctl.MultipleUpload();

               }

       

        </script>

 

我们在form中得加入如下的action

action="WebForm1.aspx?RootFolder=&Source=http%3A%2F%2Fis%2Dhezhou%3A81%2FDocument%2FForms%2FAllItems%2Easpx"

 

然后是这些隐藏的控件

<INPUT TYPE="hidden" NAME="Cmd" VALUE="Save">

<INPUT TYPE="hidden" NAME="NextUsing"

   VALUE="http://is-hezhou:81/document/Forms/AllItems.aspx">

<INPUT TYPE="hidden" VALUE="New">

<INPUT TYPE="hidden" NAME="putopts" VALUE="true">

<INPUT TYPE="hidden" NAME="destination"

   VALUE="http://is-hezhou:81/document">

<INPUT TYPE="hidden" NAME="Confirmation-URL"

   VALUE="http://is-hezhou:81/document/Forms/AllItems.aspx">

<INPUT TYPE="hidden" NAME="PostURL"

   VALUE="http://is-hezhou:81/_vti_bin/shtml.dll/_layouts/Web_SPS/WebForm1.aspx" >

<INPUT TYPE="hidden" NAME="VTI-GROUP" VALUE="0">

最后,我们只有加个<a href="javascript:UploadFiles()">Save and Close</a>,来保存就可以了。

posted on 2005-07-08 15:37  zz  阅读(584)  评论(0)    收藏  举报

导航