0

根据 DROPBOX 的说法,这应该适用于上传,但它只是上传一个文本文件,只不过是 c:/fakepath。我理解为什么会发生 fakepath 安全问题,但我不明白为什么当我直接访问 obj.value 时我什至无法让它工作

<input id="upfile_1" type="file" value="upload" name="image_1" style="display: nones" onchange="sub_1(this)"/>

function sub_1(obj){
    var file = obj.value;   

    var xhr = new XMLHttpRequest();

    xhr.upload.onprogress = function(evt) {
        var percentComplete = parseInt(100.0 * evt.loaded / evt.total);
        // Upload in progress. Do something here with the percent complete.
    };

    xhr.onload = function() {
        if (xhr.status === 200) {
            var fileInfo = JSON.parse(xhr.response);
            // Upload succeeded. Do something here with the file info.
        }
        else {
            var errorMessage = xhr.response || 'Unable to upload file';
            // Upload failed. Do something here with the error.
        }
    };

    xhr.open('POST', 'https://content.dropboxapi.com/2/files/upload');
    xhr.setRequestHeader('Authorization', 'Bearer ' + 'token');
    xhr.setRequestHeader('Content-Type', 'application/octet-stream');
    xhr.setRequestHeader('Dropbox-API-Arg', JSON.stringify({
        path: '/' +  file.name,
        mode: 'add',
        autorename: true,
        mute: false
    }));
    xhr.send(file);
}
4

0 回答 0