I am having a bit of a weird issue. I have created a visual web part for SP2010 using c# and VS2010. I finally got all the bugs worked out and it is working properly. If someone wants to attach something during the list item creation then I first create the list item then read the list item ID and attach the item. Everything works great the list item is created and the document attached. I can then open that attachment just fine.
Now here where it messes up. I want the webpart to be displayed when a user clicks the "add new item" link. Easy enough I went to the "Form web parts" in the "Cusomize List" section of the List tools ribbon. Opened the Default Display Form, minimized the default sharepoint form (I did not want to delete it as of yet) and added my Custom webpart in.
Cool now my webpart shows up when a user hits the add new item link. They fill out the form, add an attachment and hits Submit. Boom! list entry made complete with attachment woohoo right? Nope, if you try and open that attachment you get an error message as shown
Similar errors occur regardless of the file type.
I am using a FileUpload asp control to browse for the file and here is the code for attaching.
Stream fs = FileUpload1.PostedFile.InputStream;
string name = Path.GetFileName(FileUpload1.PostedFile.FileName);
byte[] contents = new byte[fs.Length];
fs.Read(contents, 0, (int)fs.Length);
fs.Close();
try
{
string addAttach = safeList.AddAttachment(safeListName, id, name, contents);
}
I searched around but did not have much luck finding an example that was specific like this. Any suggestions would be appreciated.

0 commentaires:
Enregistrer un commentaire