首页 > 解决方案 > 如何使用在 ie6 环境中运行但不一定在浏览器中运行的纯 javascript 将带有压缩文件的多/表单数据上传到 rest api

问题描述

我正在尝试在不使用表单的情况下将包含文本和压缩文件的数据上传到 rest api

数据采用混合格式,即文本和压缩文件,其中包含单个 xhtml 文件

环境:Internet explorer 6,但不在浏览器中

每次我发布数据时,我都会收到一个 http 500 状态错误,指出解压缩文件失败

我假设这可能是我尝试将压缩文件附加到正文的方式

我在下面的代码中包含了我一直在尝试做的事情。该代码不能使用任何表单或 formData 对象,因为它没有在浏览器中运行。我尝试将一些数据发送到我机器上的服务器,我可以接收数据,而且看起来文件是作为二进制文件传入的。我不太确定我尝试发送到的实时服务器是否需要 zip 文件,因为我怀疑是否可以在不尝试将其转换为二进制的情况下以原始格式发送 zip 文件。当我在浏览器中使用 formData 对象以及在 php 中执行此操作时,我有一个工作示例

    var boundary = "xxblobxx";

    var data = "";

    /** Start a new part in our body's request (Field for theSource)*/
    data += "--" + boundary;
    data += '\r\n';
    // Describe it as form data
    data += 'content-disposition: form-data; name="theSource"';
    // There's a blank line between the metadata and the data
    data += '\r\n';
    data += '\r\n';
    // Append the text data to our body's request
    data += sRelativeSource;
    //New line
    data += '\r\n';

    /** Start a new part in our body's request (Field for theparameters)*/
    data += "--" + boundary;
    data += '\r\n';
    // Describe it as form data
    data += 'content-disposition: form-data; name="theparameters"';
    // There's a blank line between the metadata and the data
    data += '\r\n';
    data += '\r\n';
    // Append the text data to our body's request
    data += sArguments;
    //New line
    data += '\r\n';

    /**Start a new part in our body's request (Attach the file)*/
    data += "--" + boundary;
    data += '\r\n';
    //Describe it as a form data
    data += 'content-disposition: form-data; name="thefile"; filename="'+ sFileNameWithExtension+'"';
    data += '\r\n';
    // And the MIME type of the file
    data += 'Content-Type: application/zip';
    // There's a blank line between the metadata and the data
    data += '\r\n';
    data += '\r\n';
    // Append the binary data to our body's request
    data += sBinaryData;
    data += '\r\n';
    data += boundary + '--';

    var xhr = new ActiveXObject('MSXML2.XMLHTTP');
    xhr.open('POST', 'https://someurl',true);
    xhr.setRequestHeader("cache-control", "no-cache");
    xhr.setRequestHeader("postman-token", "mytoken");
    xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary="+boundary);
    //xhr.setRequestHeader("Accept-Encoding", "gzip, deflate");
    xhr.setRequestHeader("Accept", "*/*");  
    xhr.onreadystatechange=function(){
        try{
            if(xhr.readyState==4){
                debugger;
                if(xhr.status == 200){
                    msg = xhr.responseText;
                    debugger;                   
                }//200
                else{
                    msg = xhr.responseText;
                }                   
            }
        }catch(e)
        {
            errorMessage(e);
        }
    }
    // And finally, send our data.
    xhr.send(data);

当我通过发送文件时,我期望返回一个二进制文件,如果转换为压缩文件,则带有 csv 文件列表。我收到错误 500 无法解压缩的事实意味着我可以访问服务器,但不太确定它是我丢失的设置还是我发送文件的格式

标签: javascriptfile-uploadxmlhttprequestcontent-type

解决方案


推荐阅读