【问题标题】:Uploading attachment while editing a SharePoint list item using Jquery not working使用 Jquery 编辑 SharePoint 列表项时上传附件不起作用
【发布时间】:2020-11-10 22:09:22
【问题描述】:

我有一个自定义 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 仍然没有帮助

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

【问题讨论】:

    标签: jquery sharepoint sharepoint-2013 office365api sharepoint-jsom


    【解决方案1】:

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

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

    我在检索后将其删除。并且成功了。

    【讨论】:

      猜你喜欢
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      相关资源
      最近更新 更多