从电子内部的angularJS SPA将电影上传到cloudinary

时间:2018-07-29 00:11:31

标签: angularjs node.js electron cloudinary

我想从用户光盘将文件从angularjs SPA的电子应用的前端上传到cloudinary。

我已经通过从input[file]中选择照片来从angularJS上传照片,但是这次我想按绝对路径将电影上传到用户光盘上的文件。

由于我使用电子,因此能够在前层内部将path用作nodeJS包,这就是我用来定义.webm文件路径的原因,因为它位于应用程序的根目录中。

$scope.uploadVideo = function () {

    var filePath = __dirname + '\\videos\\example.webm';
    console.log(filePath);

    $upload.upload({
        url: "https://api.cloudinary.com/v1_1/" + cloudinary.config().cloud_name + "/upload",
        data: {
            upload_preset: cloudinary.config().upload_preset,
            file: filePath
        }
    })
    .progress(function (info) {
        console.log(info);
    })
    .then(function (res) {
        console.log(res);
    });
};

filePath是:

  

C:\ Users \ Admin \ Documents \ ngb \ desktopApp \ videos \ example.webm

这是文件的正确路径。但是上面的代码不起作用,并出现此错误:

  

可能未处理的拒绝:{“数据”:{“错误”:{“消息”:“不支持的源URL:C:\ Users \ Admin \ Documents \ ngb \ desktopApp \ videos \ example.webm”}},“ status“:400,” config“:{” method“:” POST“,” transformRequest“:[null],” transformResponse“:

我认为我无法通过绝对路径将文件从前端发送到云端电影,但我希望我错了。我也可以对电子的nodeJS端进行相同的操作(实际上,我已经尝试过并且可以正常工作),但是在nodeJS中,我没有这个.progress回调函数,该回调函数显示有关上传进度的当前信息,这有助于在前面创建一些进度条以显示如何左长。

问题是,是否可以通过angularJS使用文件路径将其发送到cloudinary,否则,如何获取nodeJS的上传进度。 编辑我为第二个问题创建了separate question Edit2 经过更多的搜索之后,我发现来自Cloudinary合作者的information on github无法在服务器端上传,因此我在上面的链接中回答了自己的问题。


修改 因为错误是说他们不支持这样的路径,所以我放弃了在API调用中提供文件路径的想法。相反,我想将我的视频文件编码为base64格式,然后将其传递给cloudinary,因为他们说here可能:

$scope.uploadVideo = function () {

    var filePath = __dirname + '\\videos\\example.webm';
    var buff = Buffer.from(filePath).toString('base64');
    console.log(filePath);
    console.log(buff);

    $upload.upload({
        url: "https://api.cloudinary.com/v1_1/" + cloudinary.config().cloud_name + "/upload",
        data: {
            upload_preset: cloudinary.config().upload_preset,
            file: 'base64,' + buff
        }
    })
    .progress(function (info) {
        console.log(info);
    })
    .then(function (res) {
        console.log(res);
    });
};

但是它仍然无法正常工作。它可以控制台日志

  

C:\ Users \ Admin \ Documents \ ngb \ desktopApp \ videos \ example.webm

     

QzpcVXNlcnNcQm9yeXNcRG9jdW1lbnRzXGlndFxkZXNrdG9wQXBwXHZpZGVvc1xleGFtcGxlLndlYm0 =

并且错误仍然相同:

  

可能未处理的拒绝:{“ data”:{“ error”:{“ message”:“不支持的源URL:base64,QzpcVXNlcnNcQm9yeXNcRG9jdW1lbnRzXGlndFxkZXNrdG9wQXBwXHZpZGGGVY1”:“”“”“” “ POST”,”

2 个答案:

答案 0 :(得分:1)

URI必须包括实际文件内容,而不仅仅是路径。例如,使用formdata-

(function() {
    var f = document.getElementById('f');

    if (f.files.length)
        processFile();

    f.addEventListener('change', processFile, false);

    function processFile(e) {

        var f = document.getElementById('f');
        var file = f.files[0];

        console.log(file);
        var formdata = new FormData();

        formdata.append('file', file);
        //formdata.append('cloud_name', '<cloud_name>');
        formdata.append('upload_preset', '<upload_preset>');

        var xhr = new XMLHttpRequest();
        xhr.open('POST', "https://api.cloudinary.com/v1_1/<cloud_name>/upload",true);

        xhr.onload = function () {
            // do something to response
            console.log(this.responseText);
        };

        xhr.send(formdata);
    }
})();

答案 1 :(得分:0)

@rcstraus正确地指出我不是在转换为base64文件,而只是在转换为文件路径的字符串。

除了我的// POLYPRESSURE // Use this code to get polypressure messages from your MIDI keyboard. func receivedMIDIAfterTouch(_ pressure: MIDIByte, channel: MIDIChannel) { // do something } // Use this code to send your polypressure messages to the MIDI output of your application. func sendPolyPressureData(channel: MIDIChannel, noteNumber: MIDINoteNumber, value: MIDIByte) { let noteCommand: MIDIByte = MIDIByte(0xA0) + channel let message: [MIDIByte] = [noteCommand, noteNumber, value] let event = AKMIDIEvent(data: message) midi.sendEvent(event) } 不正确之外,我应该拥有// CHANNEL PRESSURE func receivedMIDIAftertouch(noteNumber: MIDINoteNumber, pressure: MIDIByte, channel: MIDIChannel) { // do something } func sendChannelPressureData(channel: MIDIChannel, value: MIDIByte) { let command: MIDIByte = MIDIByte(0xD0) + channel let message: [MIDIByte] = [command, value, 0] let event = AKMIDIEvent(data: message) midi.sendEvent(event) } // CONTROLL CHANGE & MODULATION (СС_0) func receivedMIDIController(_ controller: MIDIByte, value: MIDIByte, channel: MIDIChannel) { // do something } func ccOut (channel: MIDIChannel, cc: MIDIByte, value: MIDIByte) { let event = AKMIDIEvent(controllerChange: cc, value: value, channel: channel) midi.sendEvent(event) } // PITCH BAND func receivedMIDIPitchWheel(_ pitchWheelValue: MIDIWord, channel: MIDIChannel) { // do something } /// Send a pitch bend message. /// - Parameters: /// - value: Value of pitch shifting between 0 and 16383. Send 8192 for no pitch bending. /// - channel: Channel you want to send pitch bend message. Defaults 0. func sendPitchBendData(channel: MIDIChannel, value: UInt16) { let pitchCommand = MIDIByte(0xE0) + channel let mask: UInt16 = 0x007F let byte1 = MIDIByte(value & mask) // MSB, bit shift right 7 let byte2 = MIDIByte((value & (mask << 7)) >> 7) // LSB, mask of 127 let message: [MIDIByte] = [pitchCommand, byte1, byte2] let event = AKMIDIEvent(data: message) midi.sendEvent(event) } ,其中buff是base64中的文件,而不是path。

工作代码为:

file: data

它是node和angularJS和ng-file-upload包的组合,电子使其起作用。