Ios 使用Cordova/Phonegap通过文件传输发送录制的文件

Ios 使用Cordova/Phonegap通过文件传输发送录制的文件,ios,cordova,file-upload,media,Ios,Cordova,File Upload,Media,我正在尝试发送通过媒体插件录制的语音记录 当我尝试发送文件时,收到以下文件错误。未找到错误: Error opening file /myRecording100.wav: Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0xa358640 {NSFilePath=/myRecording100.wav, NSUnderlying

我正在尝试发送通过媒体插件录制的语音记录

当我尝试发送文件时,收到以下文件错误。未找到错误:

Error opening file /myRecording100.wav: Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0xa358640 {NSFilePath=/myRecording100.wav, NSUnderlyingError=0xa34fb30 "The operation couldn’t be completed. No such file or directory"}
2014-08-06 17:02:26.919 Bring Me[40961:c07] FileTransferError {
    code = 1;
    source = "/myRecording100.wav";
    target = "http://XXXX.xom";
}
但是,我可以在录音后播放语音录音

为什么我能够播放该文件(显示该文件已录制并正确保存),但FileTransfer无法发送该文件?

这是我的代码(用于ios):


由于File plugin的v1.0版本,要通过File transfer plugin在文件系统中上载文件,您需要使用.toURL()方法来访问它

如果要升级到文件的新(1.0.0或更新)版本,以及 您以前使用entry.fullPath作为 download()或upload(),则需要更改代码才能使用 而不是文件系统URL

FileEntry.toURL()和DirectoryEntry.toURL()返回文件系统URL 形式

因此正确的代码是:

/* Get the file and send it */
function onOK_GetFile(fileEntry) {

    var options = new FileUploadOptions();
    options.fileKey = "want";
    options.fileName = "file.wav";
    options.mimeType = "audio/wav";
    options.chunkedMode = false;
    options.params = parameters;

    var ft = new FileTransfer();
    ft.upload(fileEntry.toURL(), "https://SERVER_ADDRESS", win, fail, options);
}

我在iOS上遇到了完全相同的问题,而FileUploadOptions对我不起作用

如果有人也在挣扎,我的解决方案是切换到LocalFileSystem.Temporary

这里有一个片段显示了完整的示例(未在Android上测试):

这里有一些Worklight(调试输出)和dojo(声明),但是这段代码可以作为参考

/* Get the file and send it */
function onOK_GetFile(fileEntry) {

    var options = new FileUploadOptions();
    options.fileKey = "want";
    options.fileName = "file.wav";
    options.mimeType = "audio/wav";
    options.chunkedMode = false;
    options.params = parameters;

    var ft = new FileTransfer();
    ft.upload(fileEntry.toURL(), "https://SERVER_ADDRESS", win, fail, options);
}
var accessType = LocalFileSystem.TEMPORARY; // It was LocalFileSystem.PERSISTENT;

/** Utility function to return a fileEntry together with the metadata. */
var getFile = function(name, create, successCallback, failCallback) {

    WL.Logger.debug("Request for file " + name + " received, create is " + create + ".");

    var onSuccessFileSystem = function(fileSystem) {

        fileSystem.root.getFile(name, { create: create, exclusive: false },
            function(fileEntry){

                WL.Logger.debug("Success, file entry for " + name + " is " + JSON.stringify(fileEntry));

                fileEntry.getMetadata(function(metadata){
                    WL.Logger.debug("File entry " + name + " metadata is: " + JSON.stringify(metadata));
                    successCallback(fileEntry, metadata);
                }, function(err) {
                    WL.Logger.debug("Fail to retrieve metadata, error: " + JSON.stringify(err));
                    if(failCallback) failCallback(err);
                });

            }, 
            function(err) {
                    WL.Logger.error("Failed to retrieve the media file " + name + ".");
                    if(failCallback) failCallback(err);
            });

    }

    window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;

    window.requestFileSystem(accessType, 0, onSuccessFileSystem, function(err) {
        WL.Logger.error("Failed to access file system.");
        if(failCallback) failCallback(err);
    });

};


var Recorder = declare([  ], {

    mediaSrc : null,
    mediaObj : null,

    constructor : function(data, domNode){
        this.mediaSrc = "new_recording.wav";
    },

    startRecord : function() {

        var self = this;

        var startRecording = function(source) {

            var onMediaCallSuccess = function()  { WL.Logger.debug("Media object success."); };
            var onMediaCallError = function(err) { WL.Logger.error("Error on the media object: " + JSON.stringify(err)); };

            self.mediaObj = new Media(source, onMediaCallSuccess, onMediaCallError);
            self.mediaObj.startRecord();

        };

        // On iOS, first I need to create the file and then I can record.
        if (deviceCheck.phone.ios) {
            WL.Logger.debug("iOS detected, making sure the file exists.");
            getFile(this.mediaSrc, true, function(fileEntry){ startRecording(fileEntry.fullPath); });
        } else {
            if (!deviceCheck.phone.android)
                WL.Logger.warn("Don't know the device, trying to record ...");
            else
                WL.Logger.debug("Android detected.");
            startRecording(this.mediaSrc);
        }

    },

    stopRecord : function() {
        this.mediaObj.stopRecord();
        this.mediaObj.release();
    },

    play: function() {

        var p,
            playSuccess = function() { WL.Logger.debug("Play success."); p.release(); },
            playFail = function() { WL.Logger.debug("Play fail."); };

        p = new Media(this.mediaSrc, playSuccess, playFail);
        p.play();

    },

    getData : function(successCallback, failCallback) {

        var fileName = (deviceCheck.phone.android ? "/sdcard/" : "") + this.mediaSrc;

        WL.Logger.debug("Asking for the file entry ... ");

        getFile(this.mediaSrc, false,
                function(fileEntry, metadata) {

                    WL.Logger.debug("Success: I found a file entry: " + fileEntry.nativeURL + ", size is " + metadata.size);

                    fileEntry.file(function(file) {
                        WL.Logger.debug("Success: file retrieved!");
                        var reader = new FileReader();
                        reader.onloadend = function(evt) {
                            WL.Logger.debug("Sending content and event data to success callback.");
                            successCallback(this.result, metadata, evt);
                        };
                        reader.readAsDataURL(file);
                    }, function(err){
                        WL.Logger.error("Error: Impossible to retrieve the file");
                        failCallback(err);
                    })

                }, function(err){
                    WL.Logger.error("Fail: no file entry found: " + JSON.stringify(err));
                    failCallback(err);
                });

    }

});