简体   繁体   English

Javascript在xhttp POST请求中发送带有zip文件的元数据

[英]Javascript send metadata with zip file in xhttp POST request

I need to upload zip-files with metadata like a title and certain number.我需要上传带有元数据(如标题和特定数字)的 zip 文件。

Either I'm sending the zip file directly with:要么我直接发送 zip 文件:

function generalPostRequest(content, url, cb) {    
    var xhttp = new XMLHttpRequest();
    xhttp.open("POST", url, true);
    xhttp.withCredentials = true;
    xhttp.setRequestHeader("Authorization", "Basic " + btoa("NAME:PASS"));

    //DIFF
    xhttp.setRequestHeader("Content-Type", "application/zip");
    //DIFF

    xhttp.onreadystatechange = function () {
        if (xhttp.readyState === 4 && xhttp.status === 200) {
            if (cb) {
                cb(JSON.parse(xhttp.response));
            }
        }
    };
    xhttp.send(content);//DIFF
}

But then I don't know how to add metadata.但后来我不知道如何添加元数据。 The other way would be:另一种方式是:

function generalPostRequest(content, url, cb) {
    var xhttp = new XMLHttpRequest();
    xhttp.open("POST", url, true);
    xhttp.withCredentials = true;
    xhttp.setRequestHeader("Authorization", "Basic " + btoa("NAME:PASS"));

    //DIFF
    xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
    var params = JSON.stringify(content);
    //DIFF

    xhttp.onreadystatechange = function () {
        if (xhttp.readyState === 4 && xhttp.status === 200) {
            if (cb) {
                cb(JSON.parse(xhttp.response));
            }
        }
    };
    xhttp.send(params);//DIFF
}

But if I add the zip into an array, the JSON.stringify function removes the zip file.但是如果我将 zip 添加到数组中,JSON.stringify 函数会删除 zip 文件。 I probably have to convert it into a byte array.我可能必须将其转换为字节数组。

How do I add metadata to solution 1 or how do I convert a zip to a byte array in solution 2?如何将元数据添加到解决方案 1 或如何在解决方案 2 中将 zip 转换为字节数组?

I haven't actually tested this, and since I don't know exactly what kind of value content holds, it is difficult to construct a proper test case anyway .我还没有实际测试过这个,而且由于我不知道究竟包含什么样的价值content ,无论如何都很难构建一个合适的测试用例

You are trying to send a JSON file and a Zip file in one request.您正尝试在一个请求中发送一个 JSON 文件和一个 Zip 文件。 The way to do that is with a multipart request.这样做的方法是使用多部分请求。

XMLHttpRequest can construct a multipart request from a FormData object. XMLHttpRequest可以从FormData对象构造多部分请求。

You just need to create one and feed it the right data.您只需要创建一个并为其提供正确的数据。

var zip_data = get_zip_data();
var json_data = get_json_data();

var zip_file = new File(zip_data, "example.zip", { type: "application/zip" });
var json_file = new File(json_data, "example.json", { type: "application/json" });

var form_data = new FormData();
form_data.append("zip", zip_file, "example.zip");
form_data.append("json", json_file, "example.json");

var xhttp = new XMLHttpRequest();
xhttp.open("POST", url);
xhttp.withCredentials = true;
xhttp.setRequestHeader("Authorization", "Basic " + btoa("NAME:PASS"));
xhttp.addEventListener("load", load_handler);
xhttp.send(form_data);

function load_handler() {
    if (cb) {
        cb(JSON.parse(xhttp.response));
    }
}

Note that you must not set the Content-Type header.请注意,您不得设置 Content-Type 标头。 XHR will generate it from the FormData object automatically (and it has to do this as it needs to generate boundry markers between the files). XHR 将自动从FormData对象生成它(它必须这样做,因为它需要在文件之间生成边界标记)。

This should result in the two files being sent to the server as if you had picked them with <input type="file"> in a regular form.这应该会导致这两个文件被发送到服务器,就像您使用<input type="file">以常规形式选择它们一样。 Write the server side code accordingly (eg use $_FILES if you are using PHP or something like multer if you are using Node.js).相应地编写服务器端代码(例如,如果您使用的是 PHP,则使用$_FILES或使用 Node.js 的类似 multer 的代码)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM