是否可以使用Javascript加载特定的文件类型? [英] Is it possible to load a specific file type using Javascript?

查看:59
本文介绍了是否可以使用Javascript加载特定的文件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有使用Javascript File API加载用户选择的文件的功能,但是我想限制可以加载的文件的类型.这可能吗,我该怎么做呢?因此,例如,我只希望用户能够加载DAT文件.这是我加载文件的功能.

I have a function that is loading a user selected file using the Javascript File API, but I want to limit the type of file that can be loaded. Is this possible and how do I go about doing it? So for example, I only want the user to be able to load a DAT file. Here's my function loading the file.

function readFile(file){
    var reader = new FileReader();
    var holder;
    var holderArray = [];
    var fileArray = [];
    reader.readAsText(file, "UTF-8");
    reader.onload = function(){
        file = reader.result;
        file = file.split(/\s+/g);
        formatFile(holderArray, 3, holder, file, fileArray);
        for(var i = 0; i < 2; i++){
            formatFile(holderArray, 1, holder, file, fileArray);
        }
        for(var i = 0; i < 2; i++){
            formatFile(holderArray, 2, holder, file, fileArray);
        }
        var meh = file.length / fileArray.length;
        for(var i = 0; i < meh; i++){
            formatFile(holderArray, 5, holder, file, fileArray);
        }
        fileArray.pop();

        plume = new Plume(fileArray[0], fileArray[4], fileArray[3]);
        $("#eventDate").val(plume.dateTime);
        $("#eventLat").val(plume.startLat);
        $("#eventLon").val(plume.startLong);
        $("#eventDictionary").val(plume.dict);
        $("#eventSymbol").val(plume.symbol);

        fileArray = fileArray.splice(5);
        plume.graphic = fileArray;
    }
}

$("#load").on("click", function(){
    $("#eventNameInput").val("");
    var selectedFile = $("#selectedFile");
    selectedFile = selectedFile[0].files[0];
    if(selectedFile){
        readFile(selectedFile);
        $("#fileDetails").show();
    }
})

推荐答案

好的.您可以在"accept"属性中声明MIME类型.例如,此输入将上传图像:

Sure. You can declare MIME type in "accept" attribute For example this input will upload images :

<input type="file" name="img" accept="image/*">

对于.dat,您可以执行此操作(.dat是未知的MIME类型):

for .dat you can do this (.dat is unknown MIME type):

<input type="file" name="img" accept=".dat">

这篇关于是否可以使用Javascript加载特定的文件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆