我需要使用sharepoint webservices将文件上传到sharepoint。 是否有任何共享点服务在soap请求中使用文件内容作为base64数据?
答案 0 :(得分:3)
This资源可以帮助您理解这一点。
以下是文章中的相关内容,以防链接因未来的读者而被删除:
让我们上传文件
要上传文档,我们需要添加其他服务引用 http://server/sites/personal/_vti_bin/copy.asmx。这是复制 服务。
以下是将文档上传到文档库的代码:
CopySoapClient client = new CopySoapClient();
if (client.ClientCredentials != null)
{
client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
}
try
{
client.Open();
string url = "http://server/sites/personal/My Documents Library/Folder One/Folder Two/";
string fileName = "test.txt";
string[] destinationUrl = { url + fileName };
byte[] content = new byte[] { 1, 2, 3, 4 };
// Description Information Field
FieldInformation descInfo = new FieldInformation
{
DisplayName = "Description",
Type = FieldType.Text,
Value = "Test file for upload"
};
FieldInformation[] fileInfoArray = { descInfo };
CopyResult[] arrayOfResults;
uint result = client.CopyIntoItems(fileName, destinationUrl, fileInfoArray, content, out arrayOfResults);
Trace.WriteLine("Upload Result: " + result);
// Check for Errors
foreach (CopyResult copyResult in arrayOfResults)
{
string msg = "====================================" +
"SharePoint Error:" +
"\nUrl: " + copyResult.DestinationUrl +
"\nError Code: " + copyResult.ErrorCode +
"\nMessage: " + copyResult.ErrorMessage +
"====================================";
Trace.WriteLine(msg);
_logFactory.ErrorMsg(msg);
}
}
finally
{
if (client.State == CommunicationState.Faulted)
{
client.Abort();
}
if (client.State != CommunicationState.Closed)
{
client.Close();
}
}
答案 1 :(得分:2)
您可以使用Spservises来实现所需的功能。请参阅以下代码:
function UploadFile(listName,itemId,files){
var filereader = {},
file = {};
var fileLength = files.length;
//loop over each file selected
for(var i = 0; i < files.length; i++)
{
file = files[i];
filereader = new FileReader();
filereader.filename = file.name;
filereader.onload = function() {
var data = this.result,
n=data.indexOf(";base64,") + 8;
//removing the first part of the dataurl give us the base64 bytes we need to feed to sharepoint
data= data.substring(n);
$().SPServices({
operation: "AddAttachment",
listName: listName,
asynch: true,
listItemID:itemId,
fileName: this.filename,
attachment: data,
completefunc: testFunction(i,fileLength)
});
};
filereader.onabort = function() {
alert("The upload was aborted.");
};
filereader.onerror = function() {
alert("An error occured while reading the file.");
};
//fire the onload function giving it the dataurl
filereader.readAsDataURL(file);
}
此处files
是使用html文件控件上传返回的文件。
答案 2 :(得分:1)
我正在寻找一个可以完成工作的简单肥皂请求,并且在@ElvisLikeBear的评论的帮助下,我能够在http://server/sites/personal/_vti_bin/copy.asmx中使用CopyIntoItems Web服务。
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Body>
<CopyIntoItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<SourceUrl>fileName.ext</SourceUrl>
<DestinationUrls>
<string>{your share point location to where the file has to be uploaded.}http://mySharepoint/mysharepointsite/fileName.ext</string>
</DestinationUrls>
<Fields>
<!--FieldInformation Type="Note" DisplayName="CustomerMeta_0" InternalName="" Id="12345678-4564-9875-1236-326598745623" Value="xxx" /-->
</Fields>
<Stream>Base 64 encoded content </Stream>
</CopyIntoItems>
</SOAP:Body>
</SOAP:Envelope>