关于上传的种种(三)
<div id="cnblogs_post_body"> 上两篇说了SharePoint自带上传的简单用法,这篇讲述一下开发自定义的字段类型来做上传。所有的代码都是我昨晚写的(有BUG),没有使用公司原本的上传部件。效果如下图:http://images.cnblogs.com/cnblogs_com/Believeme/201212/20121214175305993.png
浏览状态图:
http://images.cnblogs.com/cnblogs_com/Believeme/201212/20121214175306927.png
只是实现了简单的上传功能,当然可以扩展并完善。
下面介绍用到的几个文件:
1、fldtypes_fieldupload.xml
<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?>
<FieldTypes>
<FieldType>
<Field Name=&quot;TypeName&quot;>FieldUpload</Field>
<Field Name=&quot;ParentType&quot;>Note</Field>
<Field Name=&quot;TypeDisplayName&quot;>上传</Field>
<Field Name=&quot;TypeShortDescription&quot;>上传</Field>
<Field Name=&quot;UserCreatable&quot;>TRUE</Field>
<Field Name=&quot;ShowInListCreate&quot;>TRUE</Field>
<Field Name=&quot;ShowInSurveyCreate&quot;>TRUE</Field>
<Field Name=&quot;ShowInDocumentLibraryCreate&quot;>TRUE</Field>
<Field Name=&quot;ShowInColumnTemplateCreate&quot;>TRUE</Field>
<Field Name=&quot;FieldTypeClass&quot;>SharePointProject1.FileUploadCustomField,$SharePoint.Project.AssemblyFullName$</Field>
<Field Name=&quot;FieldEditorUserControl&quot;>/_controltemplates/FileUploadProperty.ascx</Field>
<PropertySchema>
<Fields>
<Field Name=&quot;UploadDocumentLibrary&quot; DisplayName=&quot;Document Library&quot; Type=&quot;Text&quot; Hidden=&quot;True&quot;/>
</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 = &quot;&quot;;
public string UploadDocumentLibrary
{
get
{
return _UploadDocumentLibrary;
}
set
{
this.SetCustomProperty(&quot;UploadDocumentLibrary&quot;, value);
_UploadDocumentLibrary = value;
}
}
private void Init()
{
this.UploadDocumentLibrary = this.GetCustomProperty(&quot;UploadDocumentLibrary&quot;).ToStr();
}
public override void Update()
{
this.SetCustomProperty(&quot;UploadDocumentLibrary&quot;, 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(&quot;myCustom&quot;, &quot;fileup-btn&quot;).ToString();
string btnText = SPUtility.GetLocalizedString(&quot;$Resources:myCustom,fileup_btn;&quot;, &quot;myCustom&quot;, 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[&quot;filepath&quot;];
}
set
{
this.EnsureChildControls();
AddFilePath(this.ItemFieldValue.ToStr());
}
}
protected void btn_Click(object sender, EventArgs e)
{
litWarn.Text = &quot;&quot;;
if (fileup.FileName == &quot;&quot;)
{
if (ViewState[&quot;filepath&quot;] != null)
{
AddFilePath(ViewState[&quot;filepath&quot;].ToStr());
}
litWarn.Text = &quot;尚未选择上传文件&quot;;
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 = &quot;<a href='&quot; + fl.ServerRelativeUrl + &quot;'>&quot; + fileup.FileName + &quot;</a>&quot;;
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 = &quot;删除&quot;;
lbtn.Click += new EventHandler(lbtn_Click);
delcell.Controls.Add(lbtn);
filerow.Cells.Add(delcell);
htable.Rows.Add(filerow);
ViewState[&quot;filepath&quot;] = 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=&quot;wssuc&quot; TagName=&quot;InputFormControl&quot; Src=&quot;~/_controltemplates/InputFormControl.ascx&quot; %>
<%@ Register TagPrefix=&quot;wssuc&quot; TagName=&quot;InputFormSection&quot; Src=&quot;~/_controltemplates/InputFormSection.ascx&quot; %>
<wssuc:InputFormSection runat=&quot;server&quot; id=&quot;UploadControl&quot; Title=&quot;属性设置&quot;>
<template_inputformcontrols>
<wssuc:InputFormControl runat=&quot;server&quot; LabelText=&quot;Select document library&quot;>
<Template_Control>
<asp:DropDownList ID=&quot;ddlDocLibs&quot; runat=&quot;server&quot; CssClass=&quot;ms-ButtonheightWidth&quot; Width=&quot;250px&quot; />
</Template_Control>
</wssuc:InputFormControl>
</template_inputformcontrols>
</wssuc:InputFormSection>
5、FileUploadProperty.ascx.cs
public partial class FileUploadProperty : UserControl, IFieldEditor
{
protected void Page_Load(object sender, EventArgs e)
{
}
FileUploadCustomField _field = null;
public bool DisplayAsNewSection
{
get { return true; }
}
public void InitializeWithField(SPField field)
{
this._field = field as FileUploadCustomField;
}
public void OnSaveChange(SPField field, bool isNewField)
{
FileUploadCustomField myField = field as FileUploadCustomField;
myField.UploadDocumentLibrary = ddlDocLibs.SelectedItem.Value;
}
protected override void CreateChildControls()
{
base.CreateChildControls();
SPListCollection objLists = SPContext.Current.Web.Lists;
foreach (SPList objList in objLists)
{
if (objList is SPDocumentLibrary)
{
ddlDocLibs.Items.Add(new ListItem(objList.Title, objList.DefaultViewUrl));
}
}
if (!IsPostBack && _field != null)
{
if (!String.IsNullOrEmpty(_field.UploadDocumentLibrary))
{
foreach (ListItem item in ddlDocLibs.Items)
{
item.Selected = false;
}
ddlDocLibs.Items.FindByValue(_field.UploadDocumentLibrary).Selected = true;
}
}
}
}
上传完成后点击保存
http://images.cnblogs.com/cnblogs_com/Believeme/201212/20121214175306545.png
页:
[1]