使用 Web Service 上传文件至 SPS Document Library 的问题及解决: SPWeb.AllowUnsafeUpdates = true
来源:互联网 发布:混合矩阵切换器 编辑:程序博客网 时间:2024/05/12 00:09
之前有个Project 要求上传文件至Sharepint Site 中的 Document Library ,先后用两种方法处理:
1,直接使用主程序代码,但却必须放在SharePoint Server 上执行才能成功;
2,使用Web Service 方法,主程序即可随意放在那台PC 中均可调用执行上传动作。
先是使用方法1成功,但在改为方法2时,出现了问题,总是执行不成功。
原因及解决方法:
经过多方面的检查及上网Google ,最终发现竟然是由于缺少了一个设定,参看以下红色字体的代码,对SPWeb 设定:
web.AllowUnsafeUpdates = true;
但是在之前主程序中调用时,无需此设定也可以成功,当然仍然缺少不了绿色部分的代码,对SPSite 设定:
site.AllowUnsafeUpdates = true;
看来为安全起见,以后处理时,还是两处代码均需要写上才行。绿色部分的代码在一般的更新字段值的情况足以处理,但此次经验却发现有时还是需要设定 SPSite 级别的 AllowUnsafeUpdates = true 才行。
代码如下:
/// 真实使用,传入指定的 SPS Server URL / Doc Lib URL / Doc Name / Doc Binary Array
[WebMethod]
public bool UploadDocument(string p_sServer_URL,
string p_sDocLib_URL,
string p_sDocName,
Byte[] p_DocBinaryArray)
{
bool result = false;
SPSite site = null;
SPWeb web = null;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
site = new SPSite(p_sServer_URL);
site.AllowUnsafeUpdates = true;
web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
}
);
SPFolder sp_folder = web.GetFolder(p_sDocLib_URL);
if (sp_folder.Exists)
{
sp_folder.Files.Add(p_sDocName, p_DocBinaryArray, true); // overwrite if exists
sp_folder.Update();
result = true;
}
else
{
result = false;
}
}
catch (Exception ex)
{
result = false;
}
finally
{
site.Close();
site.Dispose();
web.Close();
web.Dispose();
}
return result;
}
/// 本地测试使用,传入指定的 SPS Server URL / Doc Lib URL / Doc Name / 真接打开本地文件,并产生 Doc Binary Array
//[WebMethod]
public bool UploadDocument_LocalTest(string p_sServer_URL,
string p_sDocLib_URL,
string p_sDocName)
{
bool result = false;
SPSite site = null;
SPWeb web = null;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
site = new SPSite(p_sServer_URL);
web = site.OpenWeb();
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;
}
);
SPFolder sp_folder = web.GetFolder(p_sDocLib_URL);
if (sp_folder.Exists)
{
FileStream fs = new FileStream(@"c:/Domo.txt", FileMode.Open);
byte[] p_DocBinaryArray = new byte[fs.Length];
fs.Read(p_DocBinaryArray, 0, (int)fs.Length);
sp_folder.Files.Add("Demo.txt", p_DocBinaryArray, true); // overwrite if exists
sp_folder.Update();
result = true;
}
else
{
result = false;
}
}
catch (Exception ex)
{
result = false;
}
finally
{
site.Close();
site.Dispose();
web.Close();
web.Dispose();
}
return result;
}
- 使用 Web Service 上传文件至 SPS Document Library 的问题及解决: SPWeb.AllowUnsafeUpdates = true
- 在Web Service中使用SPS Object 的相关问题?
- AXIS:使用web service进行文件的上传
- 关于SPList的Update及AllowUnsafeUpdates
- 文件上传org.springframework.web.multipart.MultipartException问题的解决
- EXT 上传文件时加上fileUpload: true出错的问题。
- Apache FTPClient上传文件时的问题及解决
- 解决vsftp无法上传文件及文件夹的问题
- FormData解决ajax上传文件的问题及ajax跨域
- 解决vsftp无法上传文件及文件夹的问题
- 打印:document.execCommand('print','true','true')的使用?
- 通过Web Service上传文件
- 在sps中使用第三方smtp软件解决邮件服务器不允许匿名访问的问题
- 使用autoit解决webdriver文件上传问题
- SharePoint web service 上传文件至文件库
- SharePoint的AllowUnsafeUpdates介绍
- 解决jsp页面元素使用disabled=true带来的不能传值到后台问题
- 【C#】解决ListView,在代码部分使用Select=true选定时的背景问题
- OOP面向对象编程——C++
- 提交到百度汉字乱码的解决办法
- 手机mac地址
- TIBCO中国研发中心职位(Offshore)
- BOSS(运营支撑)领域程序员的六层能力阶梯
- 使用 Web Service 上传文件至 SPS Document Library 的问题及解决: SPWeb.AllowUnsafeUpdates = true
- jdk 环境配量配置
- Silverlight中使用XmlReader递归读取xml文件
- 【动态规划:防卫导弹】:离散化+DP;15ms;
- struts2+jquery+ajax异步提交
- 取得 wince 剩余电量和wifi信号强度
- 优雅的LinQ
- 系统引起 磁盘被写保护
- Symbian按键处理函数:OfferKeyEventL()详解