[英]How to get mimeType from Cordova File Transfer Plugin?
我正在开发混合移动应用程序。
在一种情况下,当我们选择或上传文件时,我们需要从文件中获取mimeType。
我正在使用apache FileTransfer。
window.resolveLocalFileSystemURL(fileURI,resolveOnSuccess,resolveOnFail)
您可以从cordova File插件获取它。
$cordovaFile.checkFile(uri, '')
.then(function(entry) {
// success
var name = entry.name;
entry.file(function(data) {
// get mime type
var mime = data.type;
alert(mime);
})
}, function(error) {
// error
// show toast
});
我在TypeScript和Angular 2中使它像这样工作:
this._File.resolveLocalFilesystemUrl(somefileUri).then((entry: Entry) => {
if (entry) {
var fileEntry = entry as FileEntry;
fileEntry.file(success => {
var mimeType = success.type;
}, error => {
// no mime type found;
});
}
});
file-transfer
不会公开mimeType和其他FileUploadOptions参数。
Mimetype自动检测仅支持Windows插件代码中的上载。
这是此功能CB-5946的Jira票证 -它还提供了有关Android实现的一些建议。
在Angular 2中,我使用以下代码:
export class Plugins {
albums = {
open () : Promise<any> {
return ImagePicker.getPictures({
quality: 100,
maximumImagesCount: 1,
}).then((imgUrls) => {
return imgUrls;
}, (err) => {
if(err.error == "cordova_not_available") {
alert("Cordova is not available, please make sure you have your app deployed on a simulator or device");
} else {
console.log("Failed to open albums: " + err.error);
}
});
},
}
...
@Component({
templateUrl: 'build/pages/home/home.html',
directives: [UploadButton]
})
export class HomePage implements OnInit {
openAlbums = (): void => {
var $self = this;
this._plugins.albums.open().then((imgUrls) => {
imgUrls.forEach((imageUrl: string): void => {
if (imageUrl) {
window.resolveLocalFileSystemURL(imageUrl, function (entry: FileEntry) {
entry.file(file=> {
console.log('mimeType', file.type);
}, ((error:FileError) => console.log(error)));
});
}
});
}); };
resolveLocalFileSystemURL
通过成功回调返回一个Entry
,我必须将该Entry
转换为FileEntry
才能访问该文件方法,该方法将返回一个File
,该File
扩展了具有mime type属性的Blob
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.