问题描述
我有一个自定义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",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,"");
在检索后将其删除。而且有效。