1. <legend id='YnwVm'><style id='YnwVm'><dir id='YnwVm'><q id='YnwVm'></q></dir></style></legend>
    <tfoot id='YnwVm'></tfoot>
  2. <small id='YnwVm'></small><noframes id='YnwVm'>

      <bdo id='YnwVm'></bdo><ul id='YnwVm'></ul>
  3. <i id='YnwVm'><tr id='YnwVm'><dt id='YnwVm'><q id='YnwVm'><span id='YnwVm'><b id='YnwVm'><form id='YnwVm'><ins id='YnwVm'></ins><ul id='YnwVm'></ul><sub id='YnwVm'></sub></form><legend id='YnwVm'></legend><bdo id='YnwVm'><pre id='YnwVm'><center id='YnwVm'></center></pre></bdo></b><th id='YnwVm'></th></span></q></dt></tr></i><div id='YnwVm'><tfoot id='YnwVm'></tfoot><dl id='YnwVm'><fieldset id='YnwVm'></fieldset></dl></div>

    1. 如何使用包含文件和发布数据的 JavaScript 创建

      时间:2023-10-13

      1. <tfoot id='zDqKK'></tfoot>
          <tbody id='zDqKK'></tbody>
          <bdo id='zDqKK'></bdo><ul id='zDqKK'></ul>

          • <legend id='zDqKK'><style id='zDqKK'><dir id='zDqKK'><q id='zDqKK'></q></dir></style></legend>

              <i id='zDqKK'><tr id='zDqKK'><dt id='zDqKK'><q id='zDqKK'><span id='zDqKK'><b id='zDqKK'><form id='zDqKK'><ins id='zDqKK'></ins><ul id='zDqKK'></ul><sub id='zDqKK'></sub></form><legend id='zDqKK'></legend><bdo id='zDqKK'><pre id='zDqKK'><center id='zDqKK'></center></pre></bdo></b><th id='zDqKK'></th></span></q></dt></tr></i><div id='zDqKK'><tfoot id='zDqKK'></tfoot><dl id='zDqKK'><fieldset id='zDqKK'></fieldset></dl></div>

                <small id='zDqKK'></small><noframes id='zDqKK'>

              1. 本文介绍了如何使用包含文件和发布数据的 JavaScript 创建 AJAX 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                如何创建一个 HTTP 请求,该请求使用 PHP 服务器可以接收的 JavaScript 发送一个文件和一些 post 数据?

                How can I create a HTTP request that sends one file and some post data with JavaScript that can be received by a PHP server?

                我找到了以下建议,但似乎并不完整

                I have found the following suggestion but it does not seem to be complete

                xhr.open("POST", "upload.php");
                var boundary = '---------------------------';
                boundary += Math.floor(Math.random()*32768);
                boundary += Math.floor(Math.random()*32768);
                boundary += Math.floor(Math.random()*32768);
                xhr.setRequestHeader("Content-Type", 'multipart/form-data; boundary=' + boundary);
                var body = '';
                body += 'Content-Type: multipart/form-data; boundary=' + boundary;
                //body += '
                Content-length: '+body.length;
                body += '
                
                --' + boundary + '
                ' + 'Content-Disposition: form-data; name="';
                body += 'myfile"; filename="'+file.fileName+'" 
                ';
                body += "Content-Type: "+file.type;
                body += '
                
                ';
                body += file.getAsBinary();
                body += '
                '
                body += '--' + boundary + '
                ' + 'Content-Disposition: form-data; name="submitBtn"
                
                Upload
                ';
                body += '--' + boundary + '--';
                xhr.setRequestHeader('Content-length', body.length);
                

                为了让这个工作,我需要一个包含输入类型文件字段的文件"变量,但是在哪里放置额外的帖子数据?我也想发送描述文本.假设我还需要使用 xhr.send 来发送请求...

                To get this working I need to have a 'file' variable that contains an input type file field but where to put additional post data? I want to send a description text as well. suppose I would also need to use xhr.send to send the request...

                推荐答案

                额外的 POST 数据应该作为另一个 Content-Disposition 放置.示例:

                Additional POST data should be placed as another Content-Disposition. Example:

                Content-Type: multipart/form-data; boundary=AaB03x
                
                --AaB03x
                Content-Disposition: form-data; name="submit-name"
                
                Larry
                --AaB03x
                Content-Disposition: form-data; name="files"; filename="file1.txt"
                Content-Type: text/plain
                
                ... contents of file1.txt ...
                --AaB03x--
                

                这里发送了两个变量:要上传的文件和一个名称 = "submit-name" 且值为 Larry 的输入.您可以拥有与想要发布的变量一样多的 Content-Disposition.

                Here two variables are sent: the file to be uploaded and a input with name = "submit-name" and value Larry. You could have as many Content-Dispositions as variables you would like to POST.

                当然,如果您使用像 jQuery 这样的 js 框架,则可以简化大部分管道.这是一个 优秀插件 应该 完成工作.

                Of course much of the plumbing could be simplified if you used a js framework like jQuery. Here's an excellent plugin which should do the job.

                这篇关于如何使用包含文件和发布数据的 JavaScript 创建 AJAX 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:IE https CORS XHR 请求因 Script7002 失败:XMLHttpRequest:网 下一篇:从 GitHub 读取代码作为网页中的文本(原始)

                相关文章

                最新文章

                <tfoot id='RPDgK'></tfoot>
              2. <i id='RPDgK'><tr id='RPDgK'><dt id='RPDgK'><q id='RPDgK'><span id='RPDgK'><b id='RPDgK'><form id='RPDgK'><ins id='RPDgK'></ins><ul id='RPDgK'></ul><sub id='RPDgK'></sub></form><legend id='RPDgK'></legend><bdo id='RPDgK'><pre id='RPDgK'><center id='RPDgK'></center></pre></bdo></b><th id='RPDgK'></th></span></q></dt></tr></i><div id='RPDgK'><tfoot id='RPDgK'></tfoot><dl id='RPDgK'><fieldset id='RPDgK'></fieldset></dl></div>

                <small id='RPDgK'></small><noframes id='RPDgK'>

                <legend id='RPDgK'><style id='RPDgK'><dir id='RPDgK'><q id='RPDgK'></q></dir></style></legend>

                • <bdo id='RPDgK'></bdo><ul id='RPDgK'></ul>