Home  >  Article  >  Web Front-end  >  Angularjs uses $http to asynchronously upload Excel file method sharing

Angularjs uses $http to asynchronously upload Excel file method sharing

小云云
小云云Original
2018-02-23 10:32:221821browse

This article mainly shares with you the method of asynchronously uploading Excel files using $http in angularjs. I hope it can help you.

1. The html code of the file upload box is as follows

<form id="fileForm" enctype="multipart/form-data">
    <button id="import_asset" type="button" ng-click="import_asset()">上传文件</button>
    <input id="file_asset" type="file" style="display: none;"/>
</form>
*Note: Set the enctype attribute value of the form to: multipart/form-data

2: The js code is as follows:

$scope.import_asset = function () {
    $("#file_asset").click();
};
$("#file_asset").on("change", function(){
    var formData = new FormData();
    var file = document.getElementById("file_asset").files[0];
    if(file.name){
        var fileName = file.name.substring(file.name.lastIndexOf(".") + 1);
        if(fileName =="xlsx" || fileName =="xls"){
            formData.append('file', file);
            $http({
                method:"post",
                url:commonService.projectName + "/so/assetmanage/upload",
                data:formData,
                headers : {
                    'Content-Type' : undefined
                },
                transformRequest : angular.identity
            }).then(function (response) {
                if(response.status == 200){
                    alert("文件上传成功!!!");
                }else{
                    alert("文件上传失败!!!");
                }
            });
        }else{
            alert("文件格式不正确,请上传以.xlsx,.xls 为后缀名的文件。");
            $("#file_asset").val("");
        }
    }
});

Related recommendations:

el-upload implements detailed explanation of uploading Excel files

The above is the detailed content of Angularjs uses $http to asynchronously upload Excel file method sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn