上两篇说了SharePoint自带上传的简单用法,这篇讲述一下开发自定义的字段类型来做上传。所有的代码都是我昨晚写的(有BUG),没有使用公司原本的上传部件。效果如下图:

关于上传的种种(三)

 

浏览状态图:

关于上传的种种(三)

 

只是实现了简单的上传功能,当然可以扩展并完善。

下面介绍用到的几个文件:

1、fldtypes_fieldupload.xml

<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
  <FieldType>
    <Field Name="TypeName">FieldUpload</Field>
    <Field Name="ParentType">Note</Field>
    <Field Name="TypeDisplayName">上传</Field>
    <Field Name="TypeShortDescription">上传</Field>

    <Field Name="UserCreatable">TRUE</Field>
    <Field Name="ShowInListCreate">TRUE</Field>
    <Field Name="ShowInSurveyCreate">TRUE</Field>
    <Field Name="ShowInDocumentLibraryCreate">TRUE</Field>
    <Field Name="ShowInColumnTemplateCreate">TRUE</Field>
    <Field Name="FieldTypeClass">SharePointProject1.FileUploadCustomField,$SharePoint.Project.AssemblyFullName$</Field>
    <Field Name="FieldEditorUserControl">/_controltemplates/FileUploadProperty.ascx</Field>
    <PropertySchema>
      <Fields>
        <Field Name="UploadDocumentLibrary" DisplayName="Document Library" Type="Text" Hidden="True"/>
      </Fields>
    </PropertySchema>
  </FieldType>
</FieldTypes>

2、FileUploadCustomField.cs

public class FileUploadCustomField : SPFieldMultiLineText
    {
        public FileUploadCustomField(SPFieldCollection fields, string fieldName) : base(fields, fieldName) { Init(); }
        public FileUploadCustomField(SPFieldCollection fields, string typeName, string disName) : base(fields, typeName, disName) { Init(); }

        private string _UploadDocumentLibrary = "";
        public string UploadDocumentLibrary
        {
            get
            {
                return _UploadDocumentLibrary;
            }
            set
            {
                this.SetCustomProperty("UploadDocumentLibrary", value);
                _UploadDocumentLibrary = value;
            }
        }

        private void Init()
        {
            this.UploadDocumentLibrary = this.GetCustomProperty("UploadDocumentLibrary").ToStr();
        }

        public override void Update()
        {
            this.SetCustomProperty("UploadDocumentLibrary", this.UploadDocumentLibrary);
            this.RichText = true;
            this.RichTextMode = SPRichTextMode.FullHtml;
            base.Update();
        }


        public override BaseFieldControl FieldRenderingControl
        {
            get
            {
                BaseFieldControl fieldControl = new FileUploadCustomFieldControl();
                fieldControl.FieldName = InternalName;
                return fieldControl;
            }
        }

        public override string GetFieldValueAsHtml(object value)
        {
            return base.GetFieldValueAsHtml(HttpUtility.HtmlDecode(value.ToStr()));
        }

       
    }

3、FileUploadCustomFieldControl.cs

{
        FileUpload fileup = null;
        Button btn = null;
        Literal litWarn = null;

 

        protected override void CreateChildControls()
        {
            if (this.ControlMode != SPControlMode.Display)
            {
                HtmlTable htable = new HtmlTable();


                fileup = new FileUpload();

                //HttpContext.GetGlobalResourceObject("myCustom", "fileup-btn").ToString();

                string btnText = SPUtility.GetLocalizedString("$Resources:myCustom,fileup_btn;", "myCustom", SPContext.Current.Web.Language);

                btn = new Button();
                btn.Text = btnText;
                btn.Click += new EventHandler(btn_Click);
                litWarn = new Literal();

 


                HtmlTableCell btncell = new HtmlTableCell();
                btncell.Controls.Add(fileup);
                btncell.Controls.Add(btn);
                btncell.Controls.Add(litWarn);

                HtmlTableRow btnrow = new HtmlTableRow();

                btnrow.Cells.Add(btncell);
                htable.Rows.Add(btnrow);

                this.Controls.Add(htable);
            }

            base.CreateChildControls();
        }


        public override object Value
        {
            get
            {
                this.EnsureChildControls();

                return ViewState["filepath"];
            }
            set
            {
                this.EnsureChildControls();

                AddFilePath(this.ItemFieldValue.ToStr());
            }
        }

        protected void btn_Click(object sender, EventArgs e)
        {
            litWarn.Text = "";

            if (fileup.FileName == "")
            {
                if (ViewState["filepath"] != null)
                {
                    AddFilePath(ViewState["filepath"].ToStr());
                }


                litWarn.Text = "尚未选择上传文件";
                return;
            }

            Stream st = fileup.FileContent;

            FileUploadCustomField _field = (FileUploadCustomField)this.Field;

            SPList list = SPContext.Current.Web.GetListFromUrl(_field.UploadDocumentLibrary);

            SPDocumentLibrary spdoc = list as SPDocumentLibrary;


            SPFile fl = spdoc.RootFolder.Files.Add(fileup.FileName, st, true);

            if (fl != null)
            {
                string filepath = "<a href='" + fl.ServerRelativeUrl + "'>" + fileup.FileName + "</a>";

                AddFilePath(filepath);

            }
        }

        private void AddFilePath(string filepath)
        {
            HtmlTable htable = new HtmlTable();

            HtmlTableCell filecell = new HtmlTableCell();

            filecell.Controls.Add(new LiteralControl(filepath));

            HtmlTableRow filerow = new HtmlTableRow();
            filerow.Cells.Add(filecell);


            HtmlTableCell delcell = new HtmlTableCell();

            LinkButton lbtn = new LinkButton();
            lbtn.Text = "删除";
            lbtn.Click += new EventHandler(lbtn_Click);


            delcell.Controls.Add(lbtn);
            filerow.Cells.Add(delcell);

            htable.Rows.Add(filerow);

            ViewState["filepath"] = filepath;

            this.Controls.Add(htable);
        }

        void lbtn_Click(object sender, EventArgs e)
        {

 

        }

        protected override void RenderFieldForDisplay(System.Web.UI.HtmlTextWriter output)
        {
            output.Write(this.ItemFieldValue);
        }
    }

4、FileUploadProperty.ascx

<%@ Register TagPrefix="wssuc" TagName="InputFormControl" Src="~/_controltemplates/InputFormControl.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="InputFormSection" Src="~/_controltemplates/InputFormSection.ascx" %>
<wssuc:InputFormSection runat="server" >

相关文章:

  • 2021-11-04
  • 2022-12-23
  • 2021-09-18
  • 2022-02-10
  • 2021-08-14
猜你喜欢
  • 2021-08-22
  • 2021-04-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
  • 2022-02-25
相关资源
相似解决方案