1

I am trying to Upload a myfile.min.js to sharepoint online using JavaScript using JSOM but its stored as wrong encosing format

var req = jQuery.ajax({
             url: sourceFileItem + '?ver=' + ((new Date()) * 1), // add unique query string variable to prevent browser caching issues
             type: 'GET',
             cache: false,
             dataType: 'text',
             contentType :'text/plain; charset=UTF-8'
         }).done(function (contents) {
             ajaxDeffer.resolve({ 'url': sourceFileItem, 'contents': contents });
.................etc
var createInfo = new SP.FileCreationInformation();     
        createInfo.set_content(new SP.Base64EncodedByteArray());
        for (var i = 0; i < contents.length; i++) {
           createInfo.get_content().append(contents.charCodeAt(i));               
        }

        createInfo.set_overwrite(true);
        createInfo.set_url(sourceFileUrl.substring(sourceFileUrl.lastIndexOf('/') + 1));
 ...etc
  context.executeQueryAsync(

the original content : "False-positive on some BlackBerry 6.0 builds -

the result content for the file enter image description here

any help please ?

1

I fix it by switching to use Rest API :)

 var fileContentUrl = appweburl + "/_api/web/GetFolderByServerRelativeUrl('" + finalFolderUrl + "')/Files('" + fileName + "')/$value";


         var info = {
             url: fileContentUrl,
             method: "GET",
             //headers: { "Accept": "application/json; odata=verbose", "X-RequestDigest": formdigest },
             binaryStringResponseBody: true,
             success: function (data) {
                 //binary data available in data.body                   
                 ajaxDeffer.resolve({ 'url': sourceFileItem, 'contents': data.body });
             },
             error: function (err) {
                 ajaxDeffer.reject();
             }
         };
         executor.executeAsync(info);

 executor.executeAsync(
            {
                url: appweburl + "/_api/SP.AppContextSite(@target)/web/GetFolderByServerRelativeUrl('" + filesFolderPath + "')/Files/add(url='" + fileName + "',overwrite='true')?@target='" + hostweburl + "'",
                method: "POST",
                body: sourceFileItem.contents,
                //contentType: "application/json;odata=verbose",
                binaryStringRequestBody: true,
                timeout: 100000,
                headers: { "Accept": "application/json; odata=verbose", "X-RequestDigest": formdigest/*, "content-length": contents.length */ },
                success: function () {
                    uploadedFilesCount = uploadedFilesCount + 1;
                    var countMsg = uploadedFilesCount + ' from ' + files.length + ' files Uploaded!';
                    // ...and we're done
                    $('#' + messageElement).text('upload ' + sourceFileUrl + ' done.    ' + countMsg);
                    sourceFileItem.contents = null;
                    ajaxDeffer.resolve();

                },
                error: function (sender, args) {
                    debugger;
                    sourceFileItem.contents = null;
                    ajaxDeffer.reject();
                }
            });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.