10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

kintone ファイルのダウンロードとアップロード

Posted at

kintone で、ファイルダウンロードして、それをそのままアップロードを行う例です。

添付ファイル再利用プラグインの開発で、いくつか試して最終的にこうなりました。
Promise でファイルを扱った例があまり無かったので、メモしておきます。
現時点では、XMLHttpRequest で行うのがよさそうです。

    // ファイルダウンロード & アップロード
    return fileDownload(fileKey).then(function(resp) {
        // file upload
        return fileUpload(fileName, contentType, resp);

    }).then(function(resp){
        console.log(resp.fileKey);
        ...
    });

    // File Download
    function fileDownload(fileKey) {
        return new Promise(function(resolve, reject) {
            var url = kintone.api.url('/k/v1/file', true) + '?fileKey=' + fileKey;
            var xhr = new XMLHttpRequest();
            xhr.open('GET', url);
            xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
            xhr.responseType = 'blob';
            xhr.onload = function() {
                if (xhr.status === 200) {
                    // successful
                    resolve(xhr.response);
                } else {
                    // fails
                    reject(Error('File download error:' + xhr.statusText));
                }
            };
            xhr.onerror = function() {
                reject(Error('There was a network error.'));
            };
            xhr.send();
        });
    }


    // File upload
    function fileUpload(fileName, contentType, data) {
        return new Promise(function(resolve, reject) {
            var blob = new Blob([data], {type:contentType});
            var formData = new FormData();
            formData.append("__REQUEST_TOKEN__", kintone.getRequestToken());
            formData.append("file", blob , fileName);
            var url = kintone.api.url('/k/v1/file', true);
            var xhr = new XMLHttpRequest();
            xhr.open('POST', url);
            xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
            xhr.onload = function() {
                if (xhr.status === 200) {
                    // successful
                    var results = JSON.parse(xhr.response);
                    resolve(results);
                } else {
                    // fails
                    reject(Error('File upload error:' + xhr.statusText));
                }
            };
            xhr.onerror = function() {
                reject(Error('There was a network error.'));
            };
            xhr.send(formData);
        });
    }

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?