3

I have to implement file copying, in some case i need to copy file using CSOM (for. ex. /_layouts/15/images/itgen.png - default small list icon) into my library. What i have tried is:

  • get by SP.Web.getFileByServerRelativeUrl()
  • XMLHttpRequest/jQuery.get - but IE 9 does not support Blobs.

Is there any another way to do that? Thanks for any help!

3 Answers 3

1

You can still copy it old school way: Read file content!

    getFile: function (filePath) {
    var deferred = jQuery.Deferred();
    var req = jQuery.ajax({
        url: filePath,
        type: "GET",
        cache: false
    }).done(function (fileContents) {
        if (fileContents !== undefined && fileContents.length > 0) {
            deferred.resolve(fileContents);
        } else {
            deferred.reject('Failed to read file from web..');
        }

    }).fail(function (jqXHR, textStatus) {
        deferred.reject("Request for web file failed: " + textStatus);
    });

    return deferred.promise();
},
uploadFileToHost: function (filePath, destinationPath, fileName) {
    var deferred = jQuery.Deferred();
    getFile(filePath).then(
        function (contents) {
            var context = file.context
            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(destinationPath+fileName);
            var files = context.get_web().getFolderByServerRelativeUrl(destinationPath).get_files();
            context.load(files);
            files.add(createInfo);

            context.executeQueryAsync(
                function () {
                    deferred.resolve();
                },
                function (sender, args) {
                    deferred.reject(args.get_message());
                });
        },
        function (err) {
            deferred.reject(err);
        }
    );

    deferred.done(function () {
        Helper.logMessage("Uploaded to Host : " + file.destinationFileName);
    });
    deferred.fail(function (error) {
        Helper.logMessage("Failed to uploaded to Host : '" + file.destinationFileName + "'. Error : " + error);
    });

    return deferred.promise();
},
1
  • thanks a lot for this solution! we had to support IE9 (I wrote about it above). My solution is pretty close to yours, but i use Image element to load image and then Canvas for drawind image on it and Canvas.toDataURL() method, which IE 9 supports (oh, at least something :)).
    – Romko
    Commented Mar 24, 2015 at 15:27
1

For those searching for the answer to the TITLE of this post, no, you cannot get an SPFile (or client equivalent SP.File) that represents a file in any virtual directory (/_layouts, /_images, /_vti_bin, etc).

The reason being is that an SPFile is tied to an SPListItem, which lives in an SPList. All of these objects live in the content database (witchcraft takes place for ghosted files, but they still live in a library).

-2

here are some samples:

! CSOM move the files one folder another folder

http://www.postseek.com/meta/a6c1a29023b78254f93e543ca769e5b0 http://zimmergren.net/technical/sp-2010-uploading-files-using-the-client-om-in-sharepoint-2010

1
  • 4
    Please don't post link-only answers, instead add substantial part of the posts/blogs in your answer. Commented Mar 23, 2015 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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