[英]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"
两个 GET
和POST
这样的请求:
$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.