附件管理web服务 组件

星期三 天气:小雨转中雨
 
外面从早上 就一直在下雨
 
最近公司 将以前存在数据库中的附件 要拿出来 原因是 数据库太大了.
 
打算将附件 存到另一台服务器上 该服务器 装有Sharepoint Serices
 

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Runtime.InteropServices;

 

using Microsoft.SharePoint;
using Microsoft.SharePoint.SoapServer;

 

namespace dysoft.WSSFileServices
{
 [WebService(Namespace="http://ens.dongyu.com.cn/WSS")]
 public class CDocumentManager : System.Web.Services.WebService
 {
  private const string DEFAULT_CONTENT_TYPE = "application/unknown";
  public CDocumentManager()
  {
   InitializeComponent();
  }

 

  #region 组件设计器生成的代码
  
  //Web 服务设计器所必需的
  private IContainer components = null;
    
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
  }

 

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if(disposing && components != null)
   {
    components.Dispose();
   }
   base.Dispose(disposing);  
  }
  
  #endregion

 

  [WebMethod]
  public void Upload(string strDirName, string strFileName, byte[] bContent)
  { 

 

   if(strDirName.EndsWith("/"))
   {
    this.Upload(strDirName + strFileName, bContent);
   }
   else
   {
    this.Upload(strDirName + "/" + strFileName, bContent);
   }

 

   #region 技术调研时写的
   /*
   Microsoft.SharePoint.SPWebCollection oWebs = Microsoft.SharePoint.WebControls.SPControl.GetContextSite(this.Context).AllWebs;
   System.Text.StringBuilder strResult = new System.Text.StringBuilder();
   
   foreach(Microsoft.SharePoint.SPWeb oWeb in oWebs)
   {    
    strResult.Append(oWeb.Url + ",name=" + oWeb.Name + " : " +oWeb.Webs.Count.ToString() + "\n");
    strResult.Append("--"+oWeb.Webs.Names + "\n");
    strResult.Append("Shared Documents: " + oWeb.GetFolder("Shared Documents").Exists.ToString() + "\n");
    SPFolderCollection folders = oWeb.Folders;
    foreach (SPFolder folder in folders)
    {
     strResult.Append("  " + folder.Name + "\n");
    }
    Microsoft.SharePoint.SPWeb oCurrentWeb = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(this.Context);
    strResult.Append( oCurrentWeb.Name + "\n" + oCurrentWeb.Url);
   }
   Microsoft.SharePoint.SPWeb oIoriWeb = oWebs[0].Site.OpenWeb("/Sites/Iori/");
   SPFolderCollection folders1 = oIoriWeb.Folders;
   foreach (SPFolder folder in folders1)
   {
    strResult.Append("  " + folder.Name + "\n");
   }
   if(false)
   {
    for(int i=0; i< 100; i++)
    {
     strResult.Append(oIoriWeb.GetFolder("DocLib/TestFolder").Files.Add("test" + i.ToString("00") + ".txt", System.Text.Encoding.UTF7.GetBytes(new string('a',8*1024))));
     System.Threading.Thread.Sleep(2);
    }
   }else{
   
    Microsoft.SharePoint.SPFileCollection oFiles = oIoriWeb.GetFolder("DocLib/TestFolder").Files;
    foreach(Microsoft.SharePoint.SPFile oFile in oFiles)
    {
     oIoriWeb.GetFolder("DocLib/TestFolder").Files.Delete(oFile.Url);
    }
   }
   
   oIoriWeb.Close();
   return strResult.ToString();
   */
   #endregion
  }

 


  [WebMethod(MessageName="UploadWithFullPath")]
  public void Upload(string strFullPath, byte[] bContent)
  { 
   
   Microsoft.SharePoint.SPWeb oCurrentWeb = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(this.Context);   
   Microsoft.SharePoint.SPFolder oFolder = oCurrentWeb.GetFolder("DocLib/" + System.IO.Path.GetDirectoryName(strFullPath));
   if(oFolder != null && oFolder.Exists)
   {
    oFolder.Files.Add(System.IO.Path.GetFileName(strFullPath), bContent, true);
    return;
   }
   throw new System.IO.FileNotFoundException("指定的文件夹不存在."+"DocLib/" + System.IO.Path.GetDirectoryName(strFullPath));
   
  } 
  

 

  [WebMethod]
  public byte[] GetFile(string strDirName, string strFileName, out string strContentType)
  {
   if(strDirName.EndsWith("/"))
   {
    return this.GetFile(strDirName + strFileName, out strContentType);
   }
   else
   {
    return this.GetFile(strDirName + "/" + strFileName, out strContentType);
   }
  }
  

 

  [WebMethod(MessageName="GetFileWithFullPath")]
  public byte[] GetFile(string strFullPath, out string strContentType)
  {
   strContentType = this.GetFileType(strFullPath);
   byte[] bContent;
   Microsoft.SharePoint.SPWeb oCurrentWeb = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(this.Context);   
   Microsoft.SharePoint.SPFolder oFolder = oCurrentWeb.GetFolder("DocLib/" + System.IO.Path.GetDirectoryName(strFullPath));
   if(oFolder != null && oFolder.Exists)
   {
    try
    {
     Microsoft.SharePoint.SPFile oFile = oFolder.Files[System.IO.Path.GetFileName(strFullPath)];
     if(oFile != null && oFile.Exists)
     {     
      bContent = oFile.OpenBinary();
      return bContent;
     }
    }
    catch
    {
     return null;
    }
   }
   return null;
  }

 


  [WebMethod]
  public bool IsExist(string strFullPath)
  {
   Microsoft.SharePoint.SPWeb oCurrentWeb = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(this.Context);   
   Microsoft.SharePoint.SPFolder oFolder = oCurrentWeb.GetFolder("DocLib/" + System.IO.Path.GetDirectoryName(strFullPath));
   if(oFolder != null && oFolder.Exists)
   {
    try
    {
     Microsoft.SharePoint.SPFile oFile = oFolder.Files[System.IO.Path.GetFileName(strFullPath)];
     if(oFile != null && oFile.Exists)
     {
      return true;
     }
    }
    catch
    {
     return false;
    }
   }
   return false;
  }

 


  private string GetFileType(string strFilePath)
  {
   string strFileExtends = System.IO.Path.GetExtension(strFilePath);
   if(strFileExtends == null || strFileExtends == string.Empty)
   {
    return DEFAULT_CONTENT_TYPE;
   }
   string strValue = null;
   try
   {
    strValue = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(strFileExtends).GetValue("Content Type", DEFAULT_CONTENT_TYPE) as string;
   }
   catch
   {
    strValue = DEFAULT_CONTENT_TYPE;
   }
   
   return strValue;
  }
 }
}

Advertisements
此条目发表在SharePoint Services 2.0分类目录。将固定链接加入收藏夹。

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

You are commenting using your WordPress.com account. Log Out /  更改 )

Google+ photo

You are commenting using your Google+ account. Log Out /  更改 )

Twitter picture

You are commenting using your Twitter account. Log Out /  更改 )

Facebook photo

You are commenting using your Facebook account. Log Out /  更改 )

w

Connecting to %s