protected bool UploadFileByGroup11(string groupid, string folder, PointType pt, HttpPostedFile fu, ref string path)
{
String url = Server.MapPath(@"~/_layouts/images/revolution");
//string personalsite = webUrl + "/personal/" + Environment.UserName.Replace(".", "_");
using (SPSite site = new SPSite(webUrl))
{
using (SPWeb web = site.OpenWeb(new Guid(groupid)))
{
//设置 允许不安全的更该
// site.WebApplication.FormDigestSettings.Enabled = false;
//web.Site.WebApplication.FormDigestSettings.Enabled = false;
//site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;
string uploadPath = "";
//得到SP的文档库
SPFolder rooFolder = web.GetFolder(folder);
String fileName = Path.GetFileName(fu.FileName);
uploadPath = rooFolder.ServerRelativeUrl;
path = webUrl + uploadPath + "/" + fileName;
Response.Write(url + uploadPath + "******<br/>");
if (!Directory.Exists(url + uploadPath))
{
Response.Write(url + uploadPath + "******1111111111<br/>");
Directory.CreateDirectory(url + uploadPath);
}
if (!File.Exists(url + uploadPath + "/" + fileName))
{
Response.Write(url + uploadPath + "/" + fileName + "******222222222222<br/>");
fu.SaveAs(url + uploadPath + "/" + fileName);
}
else
{
return false;
}
try
{
foreach (SPFile file in rooFolder.Files)
{
if (file.ServerRelativeUrl == uploadPath + "/" + fileName)
{
path = webUrl + uploadPath + "/" + fileName;
//return false; //文件已经存在
}
}
//得到文件流
FileStream fStream = new FileStream(url + uploadPath + "/" + fileName, FileMode.Open);
Int32 fileCount = Convert.ToInt32(fStream.Length);
Byte[] fileContent = new Byte[fileCount];
fStream.Read(fileContent, 0, fileCount);
fStream.Close();
//保存地址
//web.Files.Add(uploadPath, contents);
//SPWeb targetWeb=web.Webs[new Guid(groupid)];
//targetWeb.AllowUnsafeUpdates = true;
//SPFolder targetFolder = targetWeb.GetFolder(folder);
//SPFolder spff = web.GetFolder("Photos");
//Response.Write(spff.Name + "|||aaaaaa<br/>");
//SPFileCollection spfcc = spff.Files;
//SPFile sdfsdfs = spfcc.Add(fileName, fileContent, true);
//SPListItem item= web.Lists["Photos"].Folders.Add(url + uploadPath + "/" + fileName, SPFileSystemObjectType.File);
//Response.Write(item.Url + "|||2222222<br/>99999999999999");
//Response.Write(rooFolder.Files.Count + "|||2222222<br/>");
SPFile file1 = rooFolder.Files.Add(fileName, fileContent, true);
//Response.Write(fileName + "|||44444<br/>");
// Response.Write(sdfsdfs.ServerRelativeUrl + "|||33333<br/>");
//SPFile file1 = targetFolder.Files.Add(fileName, fileContent, true);
//SPListItem item = targetWeb.Lists["Photos"].Items.Add("Photos", SPFileSystemObjectType.File);
//item.Update();
//targetFolder.Update();
rooFolder.Update();
web.Update();
//加分
UserInfo userM = GetCurrentUserBySession() as UserInfo;
PointActionManager.UpdatePointByUser(userM.Id);
//UserInfoService.UpdatePointByUser(userM);
path = webUrl + uploadPath + "/" + fileName;
return true;
}
catch (Exception e)
{
Response.Write(e.ToString() + "|||11111111111<br/>");
path = uploadPath + " " + e.Message;
return false;
}
}
}
}
folder取的值为"Photos"
System.IO.DirectoryNotFoundException: There is no file with URL 'Photos/Einstein test 6-02-2010.doc' in this Web. at Microsoft.SharePoint.Library.SPRequestInternalClass.PutFile(String bstrUrl, String bstrWebRelativeUrl, Object varFile, PutFileOpt PutFileOpt, String bstrCreatedBy, String bstrModifiedBy, Int32 iCreatedByID, Int32 iModifiedByID, Object varTimeCreated, Object varTimeLastModified, Object varProperties, String bstrCheckinComment, UInt32& pdwVirusCheckStatus, String& pVirusCheckMessage) at Microsoft.SharePoint.Library.SPRequest.PutFile(String bstrUrl, String bstrWebRelativeUrl, Object varFile, PutFileOpt PutFileOpt, String bstrCreatedBy, String bstrModifiedBy, Int32 iCreatedByID, Int32 iModifiedByID, Object varTimeCreated, Object varTimeLastModified, Object varProperties, String bstrCheckinComment, UInt32& pdwVirusCheckStatus, String& pVirusCheckMessage) at Microsoft.SharePoint.SPFileCollection.AddInternal(String urlOfFile, Object file, PutFileOpt fileOpt, String createdBy, String modifiedBy, Int32 createdByID, Int32 modifiedByID, DateTime timeCreated, DateTime timeLastModified, Object varProperties, String checkInComment, SPVirusCheckStatus& virusCheckStatus, String& virusCheckMessage) at Microsoft.SharePoint.SPFileCollection.Add(String urlOfFile, Byte[] file, Boolean overwrite, String checkInComment, Boolean checkRequiredFields, SPVirusCheckStatus& virusCheckStatus, String& virusCheckMessage) at Microsoft.SharePoint.SPFileCollection.Add(String urlOfFile, Byte[] file, Boolean overwrite, String checkInComment, Boolean checkRequiredFields) at Microsoft.SharePoint.SPFileCollection.Add(String urlOfFile, Byte[] file, Boolean overwrite) at Revolution.WebSite.GroupFile.UploadFileByGroup11(String groupid, String folder, PointType pt, HttpPostedFile fu, String& path