通过网络服务将文件上传到共享点的任何方式
Any way to upload a file to sharepoint through webservice
我需要使用 Sharepoint 网络服务将文件上传到 Sharepoint。
是否有任何共享点服务将文件内容作为 soap 请求中的 base64 数据使用?
This 资源将帮助您理解这一点。
以下是文章的相关内容,以防 link 为未来的读者损坏:
Let’s Upload a Document
To upload a document we need to add another service reference to
http://server/sites/personal/_vti_bin/copy.asmx. This is the Copy
service.
Here is the code to upload a document to the document library:
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();
}
}
您可以使用 Spservise 来实现所需的功能。请看下面的代码:
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文件控件上传返回的文件
我一直在寻找一个可以完成这项工作的简单 soap 请求,并且在@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>
我需要使用 Sharepoint 网络服务将文件上传到 Sharepoint。 是否有任何共享点服务将文件内容作为 soap 请求中的 base64 数据使用?
This 资源将帮助您理解这一点。
以下是文章的相关内容,以防 link 为未来的读者损坏:
Let’s Upload a Document
To upload a document we need to add another service reference to http://server/sites/personal/_vti_bin/copy.asmx. This is the Copy service.
Here is the code to upload a document to the document library:
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();
}
}
您可以使用 Spservise 来实现所需的功能。请看下面的代码:
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文件控件上传返回的文件
我一直在寻找一个可以完成这项工作的简单 soap 请求,并且在@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>