使用Jquery编辑SharePoint列表项时上载附件不起作用



我有一个自定义的SharePoint项目编辑表单。在我尝试添加附件之前,该表单适用于所有内容。它说文件已经上传,但没有上传。它在控制台上给我这个警告

jquery-1.12.4.js:10208 [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.

这是我的代码

var files= fileList.length;
//fileList is an array which has the files i need to upload
$.ajax({
url: "https://mysite/_api/web/lists/getbytitle('EventTracker')/items("+id+")",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(itemproperties),
async: false,
headers: {
"accept": "application/json;odata=verbose",  
"X-RequestDigest": $("#__REQUESTDIGEST").val(),  
"content-Type": "application/json;odata=verbose",  
"IF-MATCH": "*",  
"X-HTTP-Method": "MERGE"},
success: function (data)
{

if(files>0)
{   
for(var i=0; i<files;i++)
{
var file= fileList[i];
console.log("updated" + fileList[i]);
uploadFile(id, file);

}
$("table.tbl_jatoc").hide();
$("#thankyou").show();
}

},
error: function (fail) {
//$("#err").show();
alert("<br/> Error occured please infor the administrators");

}
}); 


});

here is the file upload function
function uploadFile(data, file)
{
var getFileBuffer = function (file) {
var deferred = $.Deferred();
var reader = new FileReader();
reader.onload = function (e) {
deferred.resolve(e.target.result);
}
reader.onerror = function (e) {
deferred.reject(e.target.error);
}
reader.readAsArrayBuffer(file);
return deferred.promise();
};
getFileBuffer(file).then(function (buffer) {
var binary = "";
var bytes = new Uint8Array(buffer);
var i = bytes.byteLength;
while (i--) {
binary = String.fromCharCode(bytes[i]) + binary;
}
var fileName = file.name;
var error = ''
$().SPServices({
operation: "AddAttachment",
async: false,
listName: "AJI Event Tracker",
listItemID: data,
fileName: fileName,
attachment: btoa(binary),
completefunc: function (xData, Status) {
console.log(file.name+" " + "uploaded");
}
});
});

}

我可以更新项目,但文件没有上传到附件中。它说上传到控制台,但给出了警告。如果我去掉文件上传部分,那么它工作得很好。我尝试将async更改为true,但仍然没有帮助

非常感谢任何帮助或指导。

对于上传附件,代码看起来很好。在我的情况下,我的ID有额外的逗号字符

id= id.replace(/%27/g,"");

我取回后就把它拿走了。它奏效了。

最新更新