简体   繁体   English

如何使用带有AngularJS的REST API将blob文件附件上传到SharePoint中的列表项?

[英]How to upload a blob file attachment to list item in SharePoint using REST API with AngularJS?

I am trying to upload a file from a HTML form as an attachment of a list item in a SharePoint 2013 list. 我正在尝试从HTML表单上载文件作为SharePoint 2013列表中列表项的附件。

Now, I can upload every file, but only .txt files aren't corrupted, so only .txt files can be opened then. 现在,我可以上传每个文件,但不会损坏.txt文件,因此只能打开.txt文件。

When user submit my form, I have an object with following properties: 用户提交表单时,我将拥有一个具有以下属性的对象: 在此处输入图片说明

Then in code, I have this to read this blob data: 然后在代码中,我要读取此Blob数据:

    var fileData = null;

    // Get a content from url with blob: .......
    $http.get(fileInfo.url, {
        headers: {
            "Accept": "application/xml;odata=verbose",
            "X-Requested-With": "XMLHttpRequest",
        }
    }).then(function (data) {
        fileData = data.data;

        // Upload a file
        $http.post(siteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items(" + itemId + ")/AttachmentFiles/add(FileName='" + fileInfo.name + "')", fileData, {
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-Requested-With": "XMLHttpRequest",
                "X-RequestDigest": document.getElementById('__REQUESTDIGEST').value
            }
        }).then(function (data) {
            console.log("OK");
            successFunction(data);
        }, function (data) {
            console.log("NOT OK");
            errorFunction(data);
        });
    }, function (data) {
        alert("Error");
    });

And probably this is the place where is the problem. 可能这就是问题所在。 When user upload a .txt file, fileData variable is string with exactly the same as in that .txt file. 当用户上传.txt文件时,fileData变量是与该.txt文件中的字符串完全相同的字符串。 But if user upload .docx file (MS Word), string in fileData variable is something like this (only the beginning): 但是,如果用户上传.docx文件(MS Word),则fileData变量中的字符串是这样的(仅是开头):

PK!ߤ lZ ?[Content_Types].xml ( n 0?E ? Ub袪* > -R {V Ǽ ?QU ↵l"%3 3Vƃ ښl w% = ^i7+ -d& 0 A 6 l4 L60# Ò S↵O X * V$z 3 3 %p)O ?^ ? PK!ߤ lZ?[Content_Types] .xml ( n 0?E ? Ub袪* >-R {V Ǽ ?QU l'' %3 3Vƃ llww% = ^ i7 + -d& 0 A 6 l4 L60# Ò S↵O X * V $z 3 3 %p)O ?^ ?

So maybe encoding? 所以也许编码? Bad specification of data type? 数据类型说明不正确? I don't know. 我不知道。

I will be extremely grateful for the help. 我将非常感谢您的帮助。 Thanks a lot in advice. 非常感谢您的建议。

To get it working you need to specify responseType: "arraybuffer" for both GET and POST requests like this: 得到它的工作,你需要指定responseType: "arraybuffer" 两个 GETPOST这样的请求:

$http.get(fileInfo.url, {
    responseType: "arraybuffer",
    headers: {
        "Accept": "application/xml;odata=verbose",
        "X-Requested-With": "XMLHttpRequest",
    }
})

and then for POST request set Content-Type to undefined and overwrite the transformRequest property so Angular doesn't encode your array into Json 然后对于POST请求,将Content-Type设置为undefined并覆盖transformRequest属性,以便Angular不会将您的数组编码为Json

$http.post(siteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items(" + itemId + ")/AttachmentFiles/add(FileName='" + fileInfo.name + "')", fileData, {
        headers: {
            "Accept": "application/json;odata=verbose",
            "Content-Type": undefined,
            "X-Requested-With": "XMLHttpRequest",
            "X-RequestDigest": document.getElementById('__REQUESTDIGEST').value
        },
        transformRequest: angular.identity,
        responseType: 'arraybuffer'
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Sharepoint - 使用 javascript 添加带有附件文件的列表项 - Sharepoint - Add list item with attachment file, using javascript 使用REST API在ExpressJS和AngularJS中上传文件 - File upload in ExpressJS and AngularJS using the REST API 无法使用 Sharepoint 2016 中的 REST API 更新列表中的列表项 - Unable to update the list item in the list using REST API in Sharepoint 2016 如何在SharePoint Hosted App中使用javascript REST API发送附件并设置电子邮件优先级? - How to send attachment and set email priority using javascript REST API in SharePoint Hosted App? 使用Angularjs中的rest api上传图片 - Image upload using rest api in Angularjs 使用 Rest API 的 Sharepoint 列表项的总和 - Sum of Sharepoint list items using Rest API 如何使用REST for Sharepoint 2013删除项目 - How to delete an item using REST for Sharepoint 2013 创建列表项 Sharepoint REST API Javascript 返回错误 500 - Creating List Item Sharepoint REST API Javascript returns Error 500 试图获取超链接列表项 SharePoint 2016 REST API - Trying to get hyperlink list item SharePoint 2016 REST API 是否可以使用REST API将项目添加到Sharepoint 2010中的站点级列表? - Is it possible to add an item to a site level list in Sharepoint 2010 with REST API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM