Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Cordova将文件从输入类型保存到文件系统=";文件";_Javascript_Cordova_Ionic - Fatal编程技术网

Javascript Cordova将文件从输入类型保存到文件系统=";文件";

Javascript Cordova将文件从输入类型保存到文件系统=";文件";,javascript,cordova,ionic,Javascript,Cordova,Ionic,如何将文件从Cordova WebView中的文件输入保存到设备文件系统 谢谢 这样想可能会有帮助: 希望,您仍在寻找答案 在我的一个应用程序中,我使用两个函数将输入文件保存为pdf格式,但您也可以将这些函数重写为其他mime类型。直到今天,这些函数都可以在Android和iOS平台上运行 函数getAppURI用于获取可将文件复制到其中的实际应用程序文件夹名称,它请求应用程序的缓存文件夹,并替换最后一个子文件夹名称以获取应用程序的基本uri,非常简单 // get your app-root-

如何将文件从Cordova WebView中的文件输入保存到设备文件系统


谢谢

这样想可能会有帮助:
希望,您仍在寻找答案

在我的一个应用程序中,我使用两个函数将输入文件保存为pdf格式,但您也可以将这些函数重写为其他mime类型。直到今天,这些函数都可以在Android和iOS平台上运行

函数
getAppURI
用于获取可将文件复制到其中的实际应用程序文件夹名称,它请求应用程序的缓存文件夹,并替换最后一个子文件夹名称以获取应用程序的基本uri,非常简单

// get your app-root-folder-name, for instance Android: file:///storage/emulated/0/Android/data/YOUR_APP_NAMESPACE/
function getAppURI(isAndroid,callback) {
    if(isAndroid) {
        window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function (filesystem) {
                var cacheDir = filesystem.root.toURL();
                var startPointCacheFolderName = cacheDir.match(/\/\w+\/$/) [0];
                callback(cacheDir.replace(startPointCacheFolderName, '') + '/');
            }, function (error) {
                console.log('no access to app-filesystem', error);
            }
        );
    }
    else{
        // iOS
        // just request the filesystem so that you really have access to it
        window.resolveLocalFileSystemURL(cordova.file.documentsDirectory,
            function(entry){
                callback(entry.nativeURL);
            },
            function(error){
            console.log("no access to filesystem",error);
        });
    }
}
实际的复制操作是通过
savePDFFromInputFile
功能完成的。此函数接受四个参数,您可以使用这些参数基本上控制目标以及复制的pdf文件的命名方式。它检查它是否是pdf,获取它的原始文件名(您可以在以后使用),并创建一个
Blob
,其中包含来自FileReader的二进制数组结果

但在复制输入文件之前,会创建一个新的空文件。之后,刚刚创建的
Blob
将写入此空文件。结束

function savePDFFromInputFile(inputHTMLElement, appURI, sourcename, callback) {
   // check whether its a pdf
   if (inputHTMLElement.files[0] && 
       inputHTMLElement.files[0].type && 
       inputHTMLElement.files[0].type.indexOf('pdf') !== - 1) {
        var filename = "";
        var reader = new FileReader();
        var fullPath = inputHTMLElement.value;
        if (fullPath) {
            // get original filename that can be used in the callback
            var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\')  : fullPath.lastIndexOf('/'));
            var filename = fullPath.substring(startIndex);
            if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
                filename = filename.substring(1);
            }
        }
        reader.onload = function () {
            // the pdf-file is read as array-buffer 
            // this array-buffer can be put into a blob
            var blob = new Blob([reader.result], {
                type: 'application/pdf'
            });
            // create empty file
            $cordovaFile.createFile(appURI, sourcename, true).then(function (success) {
                // write to this empty file
                $cordovaFile.writeExistingFile(appURI, sourcename, blob, true).then(function (success) {
                    callback({
                        name: filename,
                        source: sourcename
                    });
                }, function (error) {
                    console.log(error);
                });
            }, function (error) {
                console.log(error);
            });
        };
        reader.readAsArrayBuffer(inputHTMLElement.files[0]);
   }
}
这是如何使用这两种功能的示例:

// test for android-plattform
var isAndroid = true;
getAppURI(isAndroid, function(appURI){
    var inputFileElement = $('ID OR CLASS OF INPUT#FILE')[0]; // or use document.getElementById(...)
    var sourcename = (new Date()).getTime() + '.pdf';
    savePDFFromInputFile(inputFileElement, appURI, sourcename, function(copiedPDF){
        console.log("pdf copied successfully",copiedPDF.name,copiedPDF.source);
    });
});

希望有帮助

我不认为这有什么帮助,你为什么这么认为?不清楚你在问什么,输入文件从文件系统中选择了一个文件,所以它已经保存了,你想复制吗?是的,我想复制。你知道
inputtmlement.files[0].name
会给你文件名,对吗?这对我一点帮助都没有。是的,但我看不出有什么好处。因为你必须知道应用程序路径,在那里你可以保存一个文件,以及。