1

我想允许匿名用户在 SharePoint 中上传文件。在做了一些研究之后,我写了这个代码来允许匿名用户上传。但匿名用户无法登录并被要求登录。请帮助我。我的代码有什么问题?

    public string CreateSPFilet(string spServerURL, string tempLibrary, string folder, Stream fileStream, bool overwrite)
    {
        string strError = "";
        string fileName = "";
        try
        {                
            if (!SPSite.Exists(new Uri(spServerURL)))
            {
                strError += ERROR01;
            }
            else
            {
                SPSite tempSite = new SPSite(spServerURL);
                SPUserToken systoken = tempSite.SystemAccount.UserToken;
                using (SPSite site = new SPSite(spServerURL, systoken))
                {
                    SPWeb oWebsite = site.OpenWeb();
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                   {
                       using (SPSite elevatedSite = new SPSite(site.ID))
                       {
                           using (SPWeb eevatedWeb = elevatedSite.OpenWeb(oWebsite.ID))
                           {
                               oWebsite.AllowUnsafeUpdates = true;
                               string targetUrl = SPUrlUtility.CombineUrl(oWebsite.ServerRelativeUrl, tempLibrary);
                               SPFolder target = oWebsite.GetFolder(targetUrl);
                               SPFileCollection files = target.Files;
                               SPFile file = target.Files.Add(fileName, fileStream, overwrite);
                               oWebsite.AllowUnsafeUpdates = false;
                           }
                       }
                   });
                }
            }
4

1 回答 1

2

在最后一个 using 语句中,您需要使用对提升对象的引用而不是其他对象。像这样:

eevatedWeb.AllowUnsafeUpdates = true;   
string targetUrl = SPUrlUtility.CombineUrl(eevatedWeb.ServerRelativeUrl, tempLibrary);                                
SPFolder target = eevatedWeb.GetFolder(targetUrl);                                   
SPFileCollection files = target.Files;
SPFile file = target.Files.Add(fileName, fileStream, overwrite);                                
eevatedWeb.AllowUnsafeUpdates = false;

您还需要处理对象 tempSite 和 oWebSite,因为它们不是由您的上述代码处理的。

于 2012-04-18T08:56:58.190 回答