如何通过$ .ajax()POST发送zip文件而不使用html表单?

时间:2015-05-14 11:18:37

标签: jquery ajax spring-mvc post jszip

我使用JSZIP库文件(

)以这种方式为2 xml文件创建了一个zip文件

jszip.js

jszip-load.js

jszip-inflate.js

jszip-deflate.js

var zip = new JSZip();
zip.file("hi.xml", "<?xml version="1.0"?><root/></root></xml>");
zip.file("hello.xml", "<?xml version="1.0"?><pest/></pest></xml>");
//var content = zip.generate({ compression: "DEFLATE" });
var content=zip.generate({base64: true, binary: true, compression: "DEFLATE" });

这个压缩部分是成功的,因为我能够获得带有2个以上文件的downloads.z​​ip hi.xml&amp;我的下载文件夹中的hello.xml,如果我添加此代码,

location.href="data:application/zip;base64,"+content;

但是我在通过ajax上传这个zip文件而不使用html格式时遇到了困难。这就是我在代码中所做的,

var oBlob = new (window.BlobBuilder || window.WebKitBlobBuilder ||
                 window.MozBlobBuilder || window.MSBlobBuilder)();

        var raw = atob(content);    //decode the base64 string
        var rawLength = raw.length;
        var uInt8Array = new Uint8Array(rawLength);
        for (var i = 0; i < rawLength; ++i) { //convert to uInt8Array
            uInt8Array[i] = raw.charCodeAt(i);
        }

        oBlob.append(uInt8Array.buffer); //append it to blobbuilder
        oMyForm.append(content, oBlob.getBlob("application/zip")); //because you create a zip file, so get the zip type

        //send it
        $.ajax({
        type: 'POST',
        url: 'requestURL',//java spring servlet URL
        async: false,
        data: { 
            name:'user',
        files:oMyForm
        },
        success: function(msg){
        },
        error : function(jqXHR, textStatus, errorThrown) {
            if(typeof jqXHR == 'object'){
                if(typeof jqXHR.responseText != 'undefined'){
                    if(jqXHR.responseText == 'success'){                        

                    }
                }
            }
        }

    });

有人可以在这个问题上帮助我吗?

我也是这样做的,

var oBlob = new (window.BlobBuilder || window.WebKitBlobBuilder ||
                 window.MozBlobBuilder || window.MSBlobBuilder)();

    var raw = atob(content);    //decode the base64 string
    var rawLength = raw.length;
    var uInt8Array = new Uint8Array(rawLength);
    for (var i = 0; i < rawLength; ++i) { //convert to uInt8Array
        uInt8Array[i] = raw.charCodeAt(i);
    }

    oBlob.append(uInt8Array.buffer); //append it to blobbuilder
    oMyForm.append(content, oBlob.getBlob("application/zip")); //because you create a zip file, so get the zip type

    //send it
    var oReq = new XMLHttpRequest();
    oReq.open("POST", 'requestURL');
    oReq.send(oMyForm);

我在这里失踪的是什么?为什么zip文件没有发布到服务器?

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

除此之外,请务必使用最新的JSZip版本(例如,jszip-inflate.js不再存在)。

灵感来自How can javascript upload a blob?

var zip = new JSZip();
zip.file("hello.xml", "...");
var content = zip.generate({
    type: "blob",
    compression: "DEFLATE"
});

var fd = new FormData();
fd.append('name', 'user');
fd.append('zip', content);

$.ajax({
    type: 'POST',
    url: 'requestURL',
    data: fd,
    processData: false,
    contentType: false
}).done(function(data) {
    console.log(data);
});