понедельник, 5 сентября 2011 г.

Получение данных из asp:FileUpload и запись в ListItem.Attachment

В веб-часть добавим:

<asp:FileUpload ID="FileUpload1" runat="server"/>
   
в code behinde пишем:

ViewState["fileContents"] = GetFileContents(FileUpload1.PostedFile);

byte[] myfile = (byte[])ViewState["fileContents"];

SPListItem item = web.Lists["nameList"].Items.Add();
item["Title"]="nameListItem";
item.Attachments.Add(FileUpload1.FileName, myfile);
item.Update()
private byte[] GetFileContents(HttpPostedFile postedFile)
        {           
            HttpPostedFile file = postedFile;
            Stream fstream = file.InputStream;
            byte[] contents = new byte[fstream.Length];
            fstream.Read(contents, 0, (int)fstream.Length);
            fstream.Close();
            fstream.Dispose();
            return contents;
        }

Комментариев нет:

Отправить комментарий