• JS--------文件操作基本方法:上传/下载


    /**
     * 上传文件
     * @param {any} files 文件
     * @param {any} data 数据
     * @returns [true,文件路径]
     * @returns [false,异常/错误消息]
     */
    function fileUpload(files, data) {
        if (files.length > 0) {
            $.ajax({
                url: "../Handler/FileOperation.ashx",
                type: "post",
                data: data,
                // 告诉jQuery不要去处理发送的数据
                processData: false,
                // 告诉jQuery不要去设置Content-Type请求头
                contentType: false,
                success: function (data) {
                    var ret = eval('(' + data + ')');
                    if (ret.success) {
                        var path = ret.msg;
                        return [true, path];
                        //$("#NoticeContent").val(path).trigger("change");
                    } else {
                        return [false, ret.msg];
                        //alert(ret.msg);
                    }
                },
                error: function (data) {
                    return [false, "上传文件出错"];
                    //alert("上传文件出错!");
                }
            });
        } else {
            return [false, "请选择文件"];
        }
    
    };
    
    
    /**
     * 下载文件
     * @param {any} options
     */
    var downloadfile = function (options) {
        var config = $.extend(true, { method: 'post' }, options);
        var $iframe = $('<iframe id="down-file-iframe" />');
        var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
        $form.attr('action', config.url);
        for (var key in config.data) {
            $form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');
        }
        $iframe.append($form);
        $(document.body).append($iframe);
        $form[0].submit();
        $iframe.remove();
    }
  • 相关阅读:
    #Leetcode# 21. Merge Two Sorted Lists
    #Leetcode# 118. Pascal's Triangle
    #LeetCode# 136. Single Number
    #Leetcode# 26. Remove Duplicates from Sorted Array
    #LeetCode# 167. Two Sum II
    #Leetcode# 58. Length of Last Word
    #LeetCode# 35. Search Insert Position
    POJ 2492 J-A Bug's Life
    #Leetcode# 27. Remove Element
    【前端】.easyUI.c#
  • 原文地址:https://www.cnblogs.com/chocolatexll/p/10438354.html
Copyright © 2020-2023  润新知