doqvzh345334 2013-04-03 09:38
浏览 82
已采纳

jquery / php和文件上传到远程站点

This is my First post so please be gentle.

I am trying to streamline some B2B, but our supplier isn't helping, so I'm trying to create an interface using their standard web controls and a click by a user on my side via the browser.

The remote server has some nice security protocols so curl is out of the question. The only way I could get the remote server to accept my file was by making the interface via a web browser using their cookies. Which isn't a bad thing because I would like the staff to view the document before its posted.

When I use Fiddler and I do an upload via their site the Content Length the file size is 55344, The file size is 54920 so that's about right... But when i do it through Java / PHP / jquery they are all about 99870 in size.

If i upload a Text file this works fine.

If i upload a PDF or a Word Document the file size blows out and corrupts.

I have tried to create the post data via PHP and the jquery has a tanty and wont send, I have encoded and decoded base 64 between PHP and Java.. File size blows out

I have been googling for a few days now and not having much luck. Why would this be ?

Here is my Code for the JavaScript:

    var boundary = "' . $boundary . '";

    var jqxhr = $.ajax({
        type: "GET",
        url: "' . $filename . '",
        context: document.pdf,
        global: false,
        async:false,
        success: function(data) {
            return data;
        }
    }).responseText;


var javabody = "--" + boundary + "
"
        + "Content-Disposition: form-data; name=\"' .$name. '\"; filename=\"' .$filename. '\"
"
        + "Content-Type: application/pdf

"
        + jqxhr + "
"
        + "--" + boundary + "
"
        + "Content-Disposition: form-data; name=\"attachDocument\"" + "

"
        + "Attach Docs
"
        + "--" + boundary + "--";

    $.ajax({
            type: "post",
            url: "' . $url . '",
            datatype: "html",
            contentType: "multipart/form-data; boundary=' . $boundary . '",
            cache: "false",
            context: document.body,
            global: "false",
            data: javabody,
            crossDomain: true,
            xhrFields: {
                    withCredentials: true
                    },
            processData: false,
            async: false,
            });

Everything works, 'a' file goes up and the placeholders and the boundary's are correct, Just the file size blows out and becomes corrupt.

Any help would be appreciated

Mike

  • 写回答

1条回答 默认 最新

  • douwen7475 2013-05-06 01:41
    关注

    After brainstorming and searching this site until my fingers bled I come up with a solution:

    function submitQuoteSupportingDoc() {
        //add the binary file contents to the object, can only submit file contents via post if binary blob is present
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "' . $filename . '", true);
        xhr.responseType = "blob";
        xhr.onload = function(e) {
                if (this.status == 200) {
                        var myBlob = this.response;
                        var fd = new FormData();
                        fd.append("attachFile", myBlob, "' . $FileForUpload . '" );
                        fd.append("attachDocument", "Attach Docs");
                        $.ajax({
                            type: "POST",
                            url: "' . $DocumentUrl . '",
                            data: fd,
                            async: false,
                            crossDomain: true,
                                xhrFields: {
                                        withCredentials: true
                                        },
                            processData: false,
                            contentType: false
                        }).done(function(data) {
                               console.log(data);
                        });
                        close();
                        }
        };
        xhr.send();
    I}
    

    Obviously this is running in a PHP script.. But it works.

    Thanks Guys

    Mike

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!