SPException:它可能引用不存在的文件或文件夹,或者引用不在当前Web中的有效文件或文件夹



我在尝试将文档上传到文档库时遇到上述异常。这是我的代码:

spFile = spWeb.Files.Add(
                    docUrl,
                    uploadStream,
                    true, // overwrite or add a new version
                    uploadMessage.Metadata.CheckInComment ?? "", //check-in comment to use when creating the file in the collection. (can't be NULL)
                    false); 

从浏览器手动上载文件时不会出现此问题,但在使用SPWeb.Files从代码中将文件添加到文档库时会出现上述异常。请注意,web应用程序的内容数据库未满或未设置为只读。

我会尝试用powershell做你正在做的事情,因为输出那里的对象会让你对问题有更多的了解。

我会用稍微不同的方式来做这件事,这样我就可以确信文件正是我想要的。

var sp = new SPSite("http://localhost"); 
var site = sp.OpenWeb(); 
var folder = site.GetFolder("Documents"); 
var files = folder.Files; 
// Opening a filestream 
var fStream = File.OpenRead("C:MyDocument.docx"); 
var contents = new byte[fStream.Length]; 
fStream.Read(contents, 0, (int)fStream.Length); 
fStream.Close(); 
// Adding any metadata needed 
var documentMetadata = new Hashtable {{"Comments", "Hello World"}}; 
// Adding the file to the SPFileCollection 
var currentFile = 
    files.Add("Documents/MyDocument.docx", contents, documentMetadata, true); 
site.Dispose(); 
sp.Dispose(); 

代码不是我写的,但你可以在链接上找到它

请查看

http://zimmergren.net/technical/how-to-upload-a-filedocument-using-the-sharepoint-object-model

干杯Truez

最新更新