我开发了一个自定义的sharepoint字段,它将文件上载到特定的文档库中,并在将其保存为该字段的值后存储该文件的url。
如何避免将文件保存为附件?
谢谢您
编辑:
代码如下:
public override void UpdateFieldValueInItem()
{
string name="";
if(fileUploader.PostedFile.FileName!="")
{
//method to store the posted file in a certain library
string x= UploadFile(this.Web, fileUploader.PostedFile.FileName,fileUploader.FileName,out name);
//Stores The URL and the name of the file
SPFieldUrlValue urlValue = new SPFieldUrlValue() {
Description=name,
Url=x
};
this.Value = urlValue;
this.ItemFieldValue = urlValue;
}
}
UploadFile方法
string UploadFile(SPWeb web,string path,string fileName,out string name)
{
UploadField uf = (UploadField)base.Field;
SPList docsList = web.Lists[uf.StoreList];
SPFolder folder = web.GetFolder(docsList.RootFolder.UniqueId);
byte[] contents = File.ReadAllBytes(path);
SPFile file= folder.Files.Add(folder.Url + "/" + fileName, contents);
//folder.Update();
SPListItem item = docsList.Items[file.UniqueId];
name = item["Name"].ToString();
return getItemURL(item);
}