FileUpload到SharePoint列表附件

时间:2016-06-14 11:21:02

标签: c# asp.net sharepoint file-upload

我必须将文件附加到SharePoint列表项。我已经创建了一个带有文件上传控件的表单,但我无法使其正常工作!我可以帮忙吗?

    protected void btnSave_Click(object sender, EventArgs e)
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite site = new SPSite("http://intradev"))
            {
                using (SPWeb web = site.OpenWeb("/"))
                {
                    web.AllowUnsafeUpdates = true;

                    SPList app = web.Lists["projetoandre"];

                    SPListItemCollection collection = app.GetItems();

                    SPListItem item = collection.Add();
                    item["Title"] = txtTitle.Text;
                    item["Data"] = Convert.ToString(txtDataPretendida.Text);
                    item["Banco"] = Convert.ToString(ddlBanco.Text);
                    item["Confirmação"] = Convert.ToString(rdlUrgencia.Text);

                    //Stream fs = FileUploadControl.PostedFile.InputStream;
                    //byte[] fileContents = new byte[fs.Length];
                    //fs.Read(fileContents, 0, (int)fs.Length);
                    //fs.Close();

                    SPAttachmentCollection attachments = item.Attachments;
                    //string fileName = "Ficheiro_" + Path.GetFileName(FileUploadControl.PostedFile.FileName);
                    //attachments.Add(fileName, fileContents);

                    item.Update();

                    web.AllowUnsafeUpdates = false;
                }
            }
        });
    }

谢谢! 编辑:通过评论这些代码行,我可以填写列表但当然没有附件!

1 个答案:

答案 0 :(得分:0)

您面临的问题可能是因为SharePoint安全性。请尝试以下代码段:

$ pear list -a
Installed packages, channel __uri:
==================================
(no packages installed)

Installed packages, channel doc.php.net:
========================================
Package Version    State
pman    2015.06.19 stable

Installed packages, channel pear.geshi.org:
===========================================
Package Version  State
geshi   1.0.8.10 stable

Installed packages, channel pear.php.net:
=========================================
Package             Version State
Archive_Tar         1.4.0   stable
Console_CommandLine 1.2.0   stable
Console_Getopt      1.4.1   stable
Net_LDAP2           2.2.0   stable
PEAR                1.10.1  stable
PEAR_Manpages       1.10.0  stable
PHP_CodeSniffer     2.5.0   stable
Stream_Var          1.1.0   stable
Structures_Graph    1.1.1   stable
VersionControl_Git  0.4.4   alpha
XML_Util            1.3.0   stable

Installed packages, channel pecl.php.net:
=========================================
(no packages installed)

在用户控制文件中,在protected void btnSave_Click(object sender, EventArgs e) { SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite("http://thiswebsite")) { using (SPWeb web = site.OpenWeb("/")) { web.AllowUnsafeUpdates = true; SPList app = web.Lists["projetoandre"]; SPListItemCollection collection = app.GetItems(); SPListItem item = collection.Add(); item["Title"] = txtTitle.Text; item["Data"] = Convert.ToString(txtDataPretendida.Text); item["Banco"] = Convert.ToString(ddlBanco.Text); item["Confirmação"] = Convert.ToString(rdlUrgencia.Text); if(FileUploadControl.HasFile) { Stream fs = FileUploadControl.PostedFile.InputStream; byte[] fileContents = new byte[fs.Length]; fs.Read(fileContents, 0, (int)fs.Length); fs.Close(); SPAttachmentCollection attachments = item.Attachments; string fileName = "Ficheiro_" + Path.GetFileName(FileUploadControl.PostedFile.FileName); attachments.Add(fileName, fileContents); } item.Update(); web.AllowUnsafeUpdates = false; } } }); } 关闭之前的<ContentTemplate>标记结尾后添加以下代码段:

<asp:UpdatePanel>