当我尝试将文件上传到SharePoint 2013在线时,以下Javascript工作正常。但是,在我上传文件的同时,我一直在寻找在文档库中填充另一列的方法。例如,单行文本列,名称为“doctype”。我尝试的所有东西似乎都不符合我的代码。它有可能吗?
这是我的代码:
jQuery(document).ready(function () {
$(".uploadFile").click(function () {
fileID = $('#fileInput').attr('id');
var selectedDocCat = $('#ddlCategory').val();
var selectedDocType = $('#ddlType').val();
document.getElementById("displayName").value = mainFolderUrlVal + "-" +
selectedDocCat + '-' + selectedDocType;
fileName = $('#displayName').val();
var fileUploading = document.getElementById('fileInput');
if (fileUploading.files.length === 0) {
alert("Select a file and specify a filename!");
return;
}
var parts = fileUploading.value.split("\\");
var filename = parts[parts.length - 1];
docTitle = parts[parts.length - 1];
var fileExt = filename.split('.').pop();
var file = fileUploading.files[0];
newFileName = fileName + "." + fileExt;
console.log('filename is: ' + filename);
docCat = selectedDocCat;
uploadItems(docCat);
});
});
function uploadItems(docUplCat) {
var getArrayBuffer = getFileBuffer();
getArrayBuffer.done(function (arrayBuffer) {
var serverRelativeUrlToFolder = 'Files/' + mainFolderUrlVal + "/" +
docUplCat;
var siteUrl = _spPageContextInfo.webAbsoluteUrl;
var docLibraryEndpoint = siteUrl +
"/_api/web/getfolderbyserverrelativeurl('" + serverRelativeUrlToFolder +
"')/files/add(overwrite=true,url='" + newFileName + "')";
$.ajax({
url: docLibraryEndpoint,
type: "POST",
binaryStringRequestBody: true,
data: arrayBuffer,
timeout: 1000000,
processData: false,
state: "Update",
headers: {
"Content-Type": "application/octet-stream",
"accept": "application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
},
success: function (file) {
alert("File uploaded succesfully");
window.frameElement.cancelPopUp(); return false;
},
error: function (data) {
alert('Error: File did not upload Successfully');
}
});
});
}
function _arrayBufferToBase64(buffer) {
var binary = ''
var bytes = new Uint8Array(buffer)
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i])
}
return binary;
}
function getFileBuffer() {
var deferred = jQuery.Deferred();
var reader = new FileReader();
reader.onloadend = function (e) {
deferred.resolve(e.target.result);
}
reader.onerror = function (e) {
deferred.reject(e.target.error);
}
reader.readAsArrayBuffer($('#fileInput')[0].files[0]);
console.log('done: ' + reader);
return deferred.promise();
}
非常感谢任何帮助,谢谢!
答案 0 :(得分:0)
基本上,您需要查询使用该文件创建的列表项,并更新它的元数据。它需要多个REST查询 这是关于如何做到这一点的detailed article。
答案 1 :(得分:0)
baywet是正确的,你的上传方法很好。或者,我喜欢用直接的CSOM来做这件事。您可以使用FileCreationInformation上传附件
带有CheckIn / Check Out的示例库项目元数据更新:
using(ClientContext ctx = new ClientContext(siteUrl)) {
SecureString passWord = new SecureString();
foreach(char c in password)
passWord.AppendChar(c);
ctx.Credentials = new SharePointOnlineCredentials(username, passWord);
Web web = ctx.Web;
List list = web.Lists.GetByTitle(listName);
var query = CamlQuery.CreateAllItemsQuery();
var items = list.GetItems(query);
ctx.Load(items);
ctx.ExecuteQuery();
foreach(var item in items) {
item.File.CheckOut();
item["Category"] = "My New Category";
item["Title"] = "My New Title";
item.Update();
item.File.CheckIn("Updating Category and Title for list item", CheckinType.OverwriteCheckIn);
ctx.ExecuteQuery();
}
}