我是SharePoint的新手,我无法成功将文件上传到SharePoint。
目前,我只能将JavaScript添加到SharePoint SharePoint HTML Editor的HTML源部分,但无法调用该文件或将文件上传到SharePoint。
我怎样才能做到这一点。任何协助都会得到解决。
另外,我想参考SharePoint 2010文档。我希望能够在我的最后制作mod。
答案 0 :(得分:0)
请查看此演示:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script><script src="/Style%20Library/jquery.SPServices-0.6.2.min.js" type="application/javascript"></script><script src="/Style%20Library/jquery-1.6.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function uploadFile() {
var filePath = "c:\\test.pdf";
var url= "http://Site/Shared Documents/test.pdf";
var soapEnv =
"<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \
<soap:Body>\
<CopyIntoItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>\
<SourceUrl>" + filePath + "</SourceUrl>\
<DestinationUrls>\
<string> "+ url + "</string>\
</DestinationUrls>\
<Fields>\
<FieldInformation Type='Text' DisplayName='Title' InternalName='Title' Value='Test' />\
</Fields>\
<Stream>base64Binary</Stream>\
</CopyIntoItems>\
</soap:Body>\
</soap:Envelope>";
$.ajax({
url: "http://site/_vti_bin/copy.asmx",
beforeSend: function (xhr) { xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/CopyIntoItems"); },
type: "POST",
dataType: "xml",
data: soapEnv,
complete: processResult,
contentType: "text/xml; charset=\"utf-8\""
});
alert(soapEnv);
}
function processResult(xData, status) {
alert("Uploaded SuccessFully");
}
</script>
<input name="Upload" onclick="uploadFile()" type="button"/>
答案 1 :(得分:0)
// First locate your sharepoint site and hit service in browser.
// It is like "http://fullsitename/_vti_bin/lists.asmx"
// If you do not get 404 then it accessible and seems working for current user.
// Add its reference to project.
// SharePoint 2010 provides web service list.asmx which can be used.
// It can be used with Jquery Ajax calls as well.
// In below example i updated listitem by attaching attachment to it.
// Care fully add web reference in project or you will not get ServiceWebReference.Lists class.
// ServiceWebReference can have different name, as you prefer.
// This is working POC...... Hope this help :)
private void button1_Click(object sender, EventArgs e)
{
string srcUrl = @"C:\Users\prasads\Downloads\birthdaypic.gif";
if (!File.Exists(srcUrl))
{
throw new ArgumentException(String.Format("{0} does not exist",
srcUrl), "srcUrl");
}
FileStream fStream = File.OpenRead(srcUrl);
string fileName = fStream.Name.Substring(3);
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
ServiceWebReference.Lists listService = new ServiceWebReference.Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
try
{
string addAttach = listService.AddAttachment("mylistname", "1", fileName, contents);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
// catch error
}
}