Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Upload File or BLOB using REST API - undefined

$
0
0

Hi,

I am trying to upload a BLOB to SharePoint 2013 using REST API.  Though it uploads successfully, if I open the file, it says "undefined".  I have been readingthat BLOB can be read same as FILE so I tried to upload it using the same technique.  Not sure what I am missing here.  Here is the code:

var arrHTML = ['<b>Hi</b>'];
var myblob = new Blob(arrHTML, { type: 'text/html' });

getFileBuffer(myblob).then(uploadFile);

function getFileBuffer (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();
    }

function uploadFile (arrayBuffer) {

        var requestHeaders = {
            "Accept": "application/json; odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "content-length": arrayBuffer.byteLength
        }

       var mUrl = appweburl + "/_api/SP.AppContextSite(@target)/web/GetFolderByServerRelativeUrl('mydocs')/Files/Add(url='file.html', overwrite=true)?@target='" + hostweburl + "'";

        var executor = new SP.RequestExecutor(appweburl);
        executor.executeAsync(
            {
                url: mUrl,
                contentType: "application/json;odata=verbose",
                method: "POST",
                data: arrayBuffer,
                processData: false,
                headers: requestHeaders,
                success: function (data) {
                    alert('Document(s) uploaded successfully');                  
                },
                error: function (data, errorCode, errorMessage) {
                    alert(errorMessage);                  
                }
            }
        );
    }

**Modified Title of the question since it seems to be a generic issue**

I just tested the technique to make sure it was successfully uploading other files but came to find out that I am getting the same "undefined" in the file for any type of file.  XLSX, PPT, and other files are getting corrupted whereas doc files are empty and text and HTML files have "undefined" written in the file.  It goes to success function but the file is corrupted or contains "undefined".  Any help?  I think I would have to abandon upload using REST API.

Thanks,


kashif



Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>