Sharepoint Custom Upload Field and attachments?
I developed a custom sharepoint field that uploads a file in a specific document library and stores the url of the file after saqving it as the value of the field.
the problem is that the uploaded file is stored twice one in the library as I want and the other as an attach开发者_开发知识库ment with the item in the list that has this field.
how can I avoid savig the file as an attachment ?
thank you
Edit: Here's the Code:
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 method
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);
}
From the code I can see that, you are uploading to the library, but just ensure that there is no post back happening. Again, I would suggest, you to create the link manually. It is mostly of the form // Hope this helps
Please visit SharePointSchool.net to get started with SharePoint 2010
精彩评论