• 上传图片转为blob URL和计算文件大小


    {
    getFileUrl: function getFileUrl(fileInputId) {
        var uri = {
            url: '',
            filename: '',
            filetype: '',
            data: ''
        };
    
        var fileObj = typeof fileInputId === 'string' ? document.getElementById(fileInputId) : fileInputId;
    
        if (!utils.isCanUploadFileAsync || !fileObj) {
            return uri;
        }
        try {
            if (window.URL.createObjectURL) {
                var fileItems = fileObj.files;
                if (fileItems.length > 0) {
                    var u = fileItems.item(0);
                    uri.data = u;
                    uri.url = window.URL.createObjectURL(u);
                    uri.filename = u.name || '';
                }
            } else {
                // IE
                var u = document.getElementById(fileInputId).value;
                uri.url = u;
                var pos1 = u.lastIndexOf('/');
                var pos2 = u.lastIndexOf('\');
                var pos = Math.max(pos1, pos2);
                if (pos < 0) uri.filename = u;else uri.filename = u.substring(pos + 1);
            }
            var index = uri.filename.lastIndexOf('.');
            if (index != -1) {
                uri.filetype = uri.filename.substring(index + 1).toLowerCase();
            }
            return uri;
        } catch (e) {
            throw e;
        }
    },
    
    getFileSize: function getFileSize(file) {
        var fileSize = this.getFileLength(file);
        if (fileSize > 10000000) {
            return false;
        }
        var kb = Math.round(fileSize / 1000);
        if (kb < 1000) {
            fileSize = kb + ' KB';
        } else if (kb >= 1000) {
            var mb = kb / 1000;
            if (mb < 1000) {
                fileSize = mb.toFixed(1) + ' MB';
            } else {
                var gb = mb / 1000;
                fileSize = gb.toFixed(1) + ' GB';
            }
        }
        return fileSize;
    },
    
    getFileLength: function getFileLength(file) {
        var fileLength = 0;
        if (file) {
            if (file.files) {
              if (file.files.length > 0) {
                    fileLength = file.files[0].size;
                }
            } else if (file.select && 'ActiveXObject' in window) {
                file.select();
                var fileobject = new ActiveXObject('Scripting.FileSystemObject');
                var file = fileobject.GetFile(file.value);
                fileLength = file.Size;
            }
        }
        return fileLength;
    }
    }
  • 相关阅读:
    替代传统C/S和B/S技术的下一代客户/服务器编程技术
    理想的编程语言
    nginx进程模型
    Sybase 7.0 中文乱码问题
    netfilter 链接跟踪机制与NAT原理
    TraTraffic Server 进程模型
    nginx并发模型与traffic_server并发模型简单比较
    vs/nat原理分析
    ORACLE 查询表信息
    in 跟exists的区别
  • 原文地址:https://www.cnblogs.com/ecmasea/p/9138922.html
Copyright © 2020-2023  润新知