将 javascript Http 上传脚本转换为 Curl 命令

Converting A javascript Http Upload script to Curl command

我有一个设备在其网络界面上具有此 javascript 功能:

function upload() {
    $( "#progress" ).empty();
    $( "#uploadresult" ).empty();

    // take the file from the input
    var file = document.getElementById('files').files[0];
    var reader = new FileReader();
    reader.readAsBinaryString(file); // alternatively you can use readAsDataURL
    reader.onloadend  = function(evt)
    {
        // create XHR instance
        xhr = new XMLHttpRequest();

        // send the file through POST
        xhr.open("POST", 'upload', true);
        xhr.setRequestHeader('X-Filename', file.name);

        // make sure we have the sendAsBinary method on all browsers
        XMLHttpRequest.prototype.mySendAsBinary = function(text){
            var data = new ArrayBuffer(text.length);
            var ui8a = new Uint8Array(data, 0);
            for (var i = 0; i < text.length; i++) ui8a[i] = (text.charCodeAt(i) & 0xff);

            if(typeof window.Blob == "function")
            {
                 var blob = new Blob([data]);
            }else{
                 var bb = new (window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder)();
                 bb.append(data);
                 var blob = bb.getBlob();
            }

            this.send(blob);
        }

        // let's track upload progress
        var eventSource = xhr.upload || xhr;
        eventSource.addEventListener("progress", function(e) {
            // get percentage of how much of the current file has been sent
            var position = e.position || e.loaded;
            var total = e.totalSize || e.total;
            var percentage = Math.round((position/total)*100);

            // here you should write your own code how you wish to proces this
            $( "#progress" ).empty().append('uploaded ' + percentage + '%');
        });

        // state change observer - we need to know when and if the file was successfully uploaded
        xhr.onreadystatechange = function()
        {
            if(xhr.readyState == 4)
            {
                if(xhr.status == 200)
                {
                    // process success
                    $( "#uploadresult" ).empty().append( 'Uploaded Ok');
                }else{
                    // process error
                    $( "#uploadresult" ).empty().append( 'Uploaded Failed');
                }
            }
        };

        // start sending
        xhr.mySendAsBinary(evt.target.result);
    };
}

在我看来它正在使用 POST 上传文件,我正在尝试使用 CURL 命令行将文件上传到它,但它一直在我身上失败,这是我正在使用的命令: curl -F "FileUpload=@build.txt" myipaddress/upload

它给了我:失败(它来自服务器)

怎么了?!

好的,让我们逐步完成。

脚本将数据发布到的 url 由这一行表示:

xhr.open("POST", 'upload', true);

因此,我们知道您需要访问的端点是您的域。com/upload

我们从这一行看出:

xhr.setRequestHeader('X-Filename', file.name);

该请求正在发送带有文件名的 header,因此我们一定会包含它。

我们还看到它在发送之前将文本编码为二进制文件,因此我们将只发送实际文件,而不是先尝试读取文本或任何内容。

所以,把它们放在一起,你会得到这样的东西:

curl -H "X-Filename: yourFileName" -X POST -d @yourFileName http://yourdomain.com/upload

请注意,如果您在本地执行此操作并且没有设置主机文件,则 url 可能会替换为 ipaddress/upload。您可能还需要一个端口,具体取决于您的配置以及您是否在本地执行此操作。看起来像:ipaddress:port/upload