0

I'm trying to upload a file to a document library using the FileReader object, but failing to do so, since the files are uploaded in the wrong encoding.

This is code

$("#file").kendoUpload({
    async: {
        saveUrl: "save",
        autoUpload: true
    },
    upload: function (e) {
        $.each(e.files, function () {
            var file = this.rawFile;
            var reader = new FileReader();
            reader.onload = function (e) {
                var data = reader.result;
                var soapEnv =
                    "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='ht
                    <soap:Body>\
                        <CopyIntoItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>\
                            <SourceUrl>" + file.name + "</SourceUrl>\
                                <DestinationUrls>\
                                    <string>"+GetHostname() + folder + "/" + file.name + "</string>\
                                </DestinationUrls>\
                                <Fields>\
                                      <FieldInformation Type='Text' DisplayName='Title' InternalName='Title' Value='"+ file.name + "'  />\
                                </Fields>\
                            <Stream>" +window.btoa(data) + "</Stream>\
                        </CopyIntoItems>\
                    </soap:Body>\
                </soap:Envelope>";
                $.ajax({
                    url: "/documents/_vti_bin/copy.asmx",
                    beforeSend: function (xhr) { xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/CopyIntoItems"); },
                    type: "POST",
                    dataType: "xml",
                    data: soapEnv,
                    contentType: "text/xml; charset=\"utf-8\""
                }).then(function (data, status) {
                    var itemUrl = xmlToJSON(data).Envelope.Body.CopyIntoItemsResponse.Results.CopyResult._DestinationUrl;                           
                    return documentsUtilities.checkIn(itemUrl);
                })
                .then(function(res){
                    //notifiy user and automatically reload the grid
                });
            };
        });
        e.preventDefault();
    }

The problem im facing is located in the soap - string itself.

Passing around that data as <Stream>" +window.btoa(data) + "</Stream> results in the file being uploaded with its content as base64 encoded. Images are black and text files are unreadable.

Passing the data object itself with <Stream>" + data + "</Stream>\ results in a bad request.

What is the correct way to upload a file into document library on SharePoint 2010 using the FileReader API?

4

1 回答 1

0

FileReader API 结果是一个带有一点开销的 base64 编码字符串,它指定了实际的文件类型。如果 pdf 文件看起来像这样:

data:application/pdf;base64,JVBERi0xLjQKJdP0zOEKMSAwIG9iago8PAovQ3JlYXRpb25EYXRlKEQ6MjAxNzA5MjcxNTMz....

删除开头的位(包括 base64 之后的逗号)就是让 SharePoint 满意的全部内容:

<Stream>" + data.replace(/^.*base64,/, '') + "</Stream>\

我现在已经在 jpg、png、xlsx、docx、pdf 和 js 文件上对此进行了测试。奇迹般有效。

于 2017-11-21T10:03:09.007 回答