欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ajax上传文件 与下载

程序员文章站 2022-04-20 21:10:03
...
ajax上传

后台处理
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.util.Streams;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.okcoin.util.Logs;

@Controller
@RequestMapping(value="/file/*.do")
public class FileController extends MultiActionController{
    
    public String saveFile(HttpServletRequest request,HttpServletResponse response) throws IOException
    {
        MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartHttpServletRequest multipartRequest = null;
        try {
            multipartRequest = resolver.resolveMultipart(request);
        } catch (Exception e) {
            Logs.geterrorLogger().error("AdminAccountController submitVerifyFile 非常的请求");
        }
        
        //接收文件
        MultipartFile documentFile = multipartRequest.getFile("file");
        //获取文件名
        String documentFileName=documentFile.getOriginalFilename();
        System.out.println("FileName:" + documentFileName);
        
        BufferedInputStream in = new BufferedInputStream(documentFile.getInputStream());
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("D:\\Users\\" + documentFileName));
        Streams.copy(in, out, true);
        System.out.println("copy finished.");
        return "";
    }
}

前端处理
function UpladFile() {
    var fileObj = document.getElementById("file").files[0]; // js 获取文件对象
    var FileController = "地址"; // 接收上传文件的后台地址

    // FormData 对象
    var form = new FormData();
    form.append("file", fileObj); // 文件对象
    // XMLHttpRequest 对象
    var xhr = new XMLHttpRequest();
    xhr.open("post", FileController, true);
    xhr.onload = function() {
        // alert("上传完成!");
    };
    xhr.send(form);
}




ajax 生成表格下载


//生成csv文件
function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) { 
    var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
    var CSV = '';
    if (ShowLabel) {
        var row = "";
        for (var index in arrData[0]) {
            row += index + ',';
        }
        row = row.slice(0, -1);
        CSV += row + '\r\n';
    }
    for (var i = 0; i < arrData.length; i++) {
        var row = "";
        for (var index in arrData[i]) {
            row += '"' + arrData[i][index] + '",';
        }
        row.slice(0, row.length - 1);
        CSV += row + '\r\n';
    }
    if (CSV == '') {
        alert("Invalid data");
        return;
    }
    var blob = new Blob(["\ufeff" + CSV], {type: 'text/csv'});
    var fileName = "";
    fileName += ReportTitle.replace(/ /g, "_");
    var uri = 'data:text/csv;charset=utf-8,\uFEFF' + encodeURI(CSV);
    var link = document.createElement("a");
    link.href = URL.createObjectURL(blob);
    link.style = "visibility:hidden";
    link.download = fileName + ".csv";
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}