我有一个将文件上传到服务器的应用程序。在onSelectFile
函数中,我检查文件类型以查看它是否有效。我还有FileFilter
限制用户可以选择的内容,但我想仔细检查他们是否正在上传正确的文件类型。
在PC上,我可以获得该文件的type
值,但在Mac(Safari,Firefox,Chrome)上,我得到NULL。
这是我尝试获取类型的函数
// Called when a file is selected
private function onSelectFile(event:Event):void {
//let's see if we're dealing with a new album or an existing album
var arrFoundList:Array = new Array();
// Get list of files from fileList, make list of files already on upload list
for (var i:Number = 0; i < _arrUploadFiles.length; i++) {
for (var j:Number = 0; j < _refAddFiles.fileList.length; j++) {
if (_arrUploadFiles[i].name == _refAddFiles.fileList[j].name) {
arrFoundList.push(_refAddFiles.fileList[j].name);
_refAddFiles.fileList.splice(j, 1);
j--;
}
}
}
if (_refAddFiles.fileList.length >= 1) {
for (var k:Number = 0; k < _refAddFiles.fileList.length; k++) {
var fileType:String = _refAddFiles.fileList[k].type;
Alert.show("File type: " + fileType.toString() + " | name: " + _refAddFiles.fileList[k].name); //fileType is always NULL on Mac
var validTypes:Array = new Array(".jpg", ".jpeg", ".gif", ".png", ".JPG", ".JPEG", ".GIF", ".PNG");
if (validTypes.indexOf(fileType.toLowerCase()) < 0)
{
Alert.show("The file type: " + fileType + " is not valid (file: " + _refAddFiles.fileList[k].name + "). Valid types are .jpg, .gif, and .png", "Invalid File Type");
continue;
}
//start the upload for each file
var item:OneFile= new OneFile();
item.fileName=_refAddFiles.fileList[k].name;
item.fileSize=_refAddFiles.fileList[k].size;
item.file=_refAddFiles.fileList[k];
_bytesTotal+=_refAddFiles.fileList[k].size;
if (existingalbum.selected)
{
item.album=albums.selectedItem.@aid;
}
fileList.addChild(item);
_files.addItem(item);
_numFiles++;
}
startUpload();
}
else
{
Alert.show("No files were added to the list to be uploaded.");
}
if (arrFoundList.length >= 1) {
Alert.show("The file(s): \n\n• " + arrFoundList.join("\n• ") + "\n\n...are already on the upload list. Please change the filename(s) or pick a different file.", "File(s) already on list");
}
else{ // some comment
}
}
答案 0 :(得分:0)
以下是我找到的解决方法:
public static function extractFileType(fileName:String):String {
var extensionIndex:Number = fileName.lastIndexOf(".");
if (extensionIndex == -1) {
return "";
} else {
return "." + fileName.substr(extensionIndex + 1,fileName.length);
}
}
http://www.josh-ho.com/as3-mac-filereference-type-workaround/