• .Net使用HttpClient以multipart/formdata形式post上传文件及其相关参数 不及格的程序员


    .Net使用HttpClient以multipart/form-data形式post上传文件及其相关参数

     

    文章正文:

    前言:

      本次要讲的是使用.Net HttpClient拼接multipark/form-data形式post上传文件和相关参数,并接收到上传文件成功后返回过来的结果(图片地址,和是否成功)。可能有很多人会说用ajax不是就可以轻松的实现吗?的确是在不存在跨域问题的前提下使用ajax上传文件,接收返回结果是最佳的选择。无奈的是我们对接的是第三方的一个上传图片的接口,而且对方并没有对我们的域名设置允许跨域,为了能够解决这一问题我们只能够通过后端请求避免跨域问题。

    什么是multipart/form-data请求:

    关于multipart/form-data详情查看: https://www.cnblogs.com/tylerdonet/p/5722858.html

    Html上传图片按钮:

    <div class="cover-hd">
    <a href="javascript:;" class="a-uploadCustom">
    <input type="file" id="Logoimg" onchange="OnchangeImage(this)" /></a>
    </div>

    使用ajax将图片文件流和相关参数传递到后端进行拼接:

    注意:因为我这里调用第三方接口需要传递(appid应用程序唯一标识,random随机数,和sign签名)

    复制代码
    <script type="text/javascript">
        //后端图片上传
        function OnchangeImage(obj) {
            var formData = new FormData();
            var files = $(obj).prop('files'); //获取到文件列表
            console.log(files[0]);
            formData.append("imgType", 1);
            formData.append("appId","你需要传递的参数");
            formData.append("random", "你需要传递的参数");
            formData.append("file", files[0]);//图片文件流
            formData.append("sign", "你需要传递的参数");
    
            console.log(formData);
            jQuery.support.cors = true;
            $.ajax({
                async: true,
                contentType: false, //头部请求内容格式
                dataType: 'json',
                type: 'post',
                data:formData,
                // 告诉jQuery不要去处理发送的数据
                processData: false,
                url: "@Url.Action("ImageUpload", "MtVirtualStore")",//后端接收图片接口
                success: function(data) {
                    //后端Httpclient请求成功后返回过来的结果
                    console.log(data);
                }
            });
        }
    </script>
    复制代码

    后端接收图片和参数,并将图片文件流转化为图片字节类型数据:

    复制代码
    //接收前端图片文件信息
    [HttpPost]
    public JsonResult ImageUpload(FormContext context)
    {
    HttpPostedFileBase fileData = Request.Files[0];
    string appId=Request["appId"];
    string random=Request["random"];
    string sign=Request["sign"];
    string imgType=Request["imgType"];
    if (fileData != null)
    {
    try{
    string fileName = Path.GetFileName(fileData.FileName);//原始文件名称
    byte[] byteFileData = ReadFileBytes(fileData);//文件流转为字节流
    
    var resultContext =HttpClientPostUpload(byteFileData,appId,random,sign,imgType, fileName);
    
    return Json(new { code = 1, list = resultContext,msg="上传成功~"});
    }
    catch (Exception ex)
    {
    return Json(new { code = 0, msg = ex.Message });
    }
    }
    else
    {
    return Json(new { code = 0, msg = "图片上传失败,请稍后再试~" });
    }
    }
            
    //文件流转化为字节
    /// <summary>
    /// 文件流类型转化字节类型
    /// </summary>
    /// <param name="fileData">文件流数据</param>
    /// <returns></returns>
    private byte[] ReadFileBytes(HttpPostedFileBase fileData)
    {
    byte[] data;
    using (Stream inputStream = fileData.InputStream)
    {
    MemoryStream memoryStream = inputStream as MemoryStream;
    if (memoryStream == null)
    {
    memoryStream = new MemoryStream();
    inputStream.CopyTo(memoryStream);
    }
    data = memoryStream.ToArray();
    }
    return data;
    }
    复制代码

    重点,HttpClient拼接multipart/form-data形式参数post提交数据:

    复制代码
    /// <summary>
    /// 向目标地址提交图片文件参数数据
    /// </summary>
    /// <param name="bmpBytes">图片字节流</param>
    /// <param name="appId">appid</param>
    /// <param name="random">随机数</param> 
    /// <param name="sign">签名</param>
    /// <param name="imgType">上传图片类型</param>     
    /// <param name="fileName">图片名称</param>
    /// <returns></returns>
    public string HttpClientPostUpload(byte [] bmpBytes, string appId, string random,,string sign,string imgType,string fileName)
    {
    using (var client = new HttpClient())
    {
    List<ByteArrayContent> list = new List<ByteArrayContent>();
    
    var dataContent = new ByteArrayContent(Encoding.UTF8.GetBytes(appId));
    dataContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")//内容处置标头
    {
    Name = "appId"
    };
    list.Add(dataContent);
    
    var dataContent2 = new ByteArrayContent(Encoding.UTF8.GetBytes(imgType));
    dataContent2.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
    {
    Name = "imgType"
    };
    list.Add(dataContent2);
    
    var dataContent3 = new ByteArrayContent(Encoding.UTF8.GetBytes(random));
    dataContent3.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
    {
    Name = "random"
    };
    list.Add(dataContent3);
    
    var dataContent4 = new ByteArrayContent(Encoding.UTF8.GetBytes(sign));
    dataContent4.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
    {
    Name = "sign"
    };
    list.Add(dataContent4);
    List<ByteArrayContent> list2 = new List<ByteArrayContent>();
    
    var fileContent = new ByteArrayContent(bmpBytes);//填充图片字节
    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
    {
    Name="file",
    FileName=fileName
    };
    list.Add(fileContent);
    
    using (var content =new MultipartFormDataContent())
    {
    Action<List<ByteArrayContent>> act = (dataContents) =>
    {//声明一个委托,该委托的作用就是将ByteArrayContent集合加入到MultipartFormDataContent中
    foreach (var byteArrayContent in dataContents)
    {
    content.Add(byteArrayContent);
    }
    };
    
    act(list);//执行act
    try
    {
    var result = client.PostAsync("https://xxxxxx.com/imageUpload/", content).Result;//post请求
    return result.Content.ReadAsStringAsync().Result;
    }
    catch (Exception ex)
    {
    return ex.Message;
    }
    
    }
    }
    }
    复制代码

    使用Fiddler 4 抓包查看请求的参数:

    因为我们没有办法看到我们所拼接成功后的multipark/form-data形式的数据,想要看到对应拼接的请求参数可以使用 Fiddler 4 抓包工具查看:

    关于Fiddler 4抓包工具的使用可以阅读该篇博客:https://www.jianshu.com/p/55f7be58a7e4

    抓包获取到的multipark/form-data形式的请求参数如下图:

    总结:

      写到最后才发现,原本只需要一个简单的请求就可以解决的问题因为跨域把这个问题变得如此繁琐,搞得真叫人蛋痛。这里我试过了很多种方式拼接multipark/form-data形式的请求参数,最后在坚持不懈的尝试下终于成功了


    问题描述:在使用jQuery封装的ajax请求的时候,用于上传图片,表单等一系列。里面formData方法传入作为data的属性值的时候,会出现这个错误

     
    Illegal invocation

    在网上百度了一下,只需要添加两行代码就可以解决了。
    contentType: false,
    // 告诉jQuery不要去设置Content-Type请求头
    processData: false,
    // 告诉jQuery不要去处理发送的数据
    

    contentType

    告诉服务器从浏览器提交过来的数据格式。

    默认值是:contentType = "application/x-www-form-urlencoded"

    如果不设置的话,我们查看Request headers里面的内容
     
    image.png

    如果为contentType:false的话,
     
    image.png

    如果使用formData的方式提交表单的话,会默认 contentType:'multipart/form-data'
    所以这到底是什么意思呢?

    这是分界符,后面的一堆字符串是随机生成的,目的是防止上传文件中出现分界符导致服务器无法正确识别文件起始位置。如果为true的时候,jq就会去操作这个分界符,导致后台服务器无法操作数据,导致请求失败。

    processData

    默认为true,规定通过请求发送的数据是否转换为查询字符串,默认情况下,通过data选项传递进来的数据,如果是一个对象(技术上讲只要不是字符串),都会处理转化成一个查询字符串,以配合默认内容类型"application/x-www-form-urlencoded"。如果要发送 DOM 树信息或其它不希望转换的信息,请设置为 false。

    总结来说

    本次项目中用到了jQuery封装的ajax请求,并结合了formData的形式来传输数据,因为用了formData会默认 contentType:'multipart/form-data',如果不用Content-Type: application/x-www-form-urlencoded,为了保证后台能够解析浏览器传输过去的数据的时候,我们需要设置contentType:false(防止jq去操作,失去这个分界符)。

    借鉴:https://segmentfault.com/a/1190000007207128


    Submitting forms and uploading filesSection
    Instances of XMLHttpRequest can be used to submit forms in two ways:
    
    using only AJAX
    using the FormData API
    Using the FormData API is the simplest and fastest, but has the disadvantage that data collected can not be stringified.
    Using only AJAX is more complex, but typically more flexible and powerful.
    
    Using nothing but XMLHttpRequestSection
    Submitting forms without the FormData API does not require other APIs for most use cases. The only case where you need an additional API is if you want to upload one or more files, where you use the FileReader API.
    
    A brief introduction to the submit methods
    An html <form> can be sent in four ways:
    
    using the POST method and setting the enctype attribute to application/x-www-form-urlencoded (default);
    using the POST method and setting the enctype attribute to text/plain;
    using the POST method and setting the enctype attribute to multipart/form-data;
    using the GET method (in this case the enctype attribute will be ignored).
    Now, consider the submission of a form containing only two fields, named foo and baz. If you are using the POST method the server will receive a string similar to one of the following three examples, depending on the encoding type you are using:
    
    Method: POST; Encoding type: application/x-www-form-urlencoded (default):
    
    Content-Type: application/x-www-form-urlencoded
    
    foo=bar&baz=The+first+line.%0D%0AThe+second+line.%0D%0A
    Method: POST; Encoding type: text/plain:
    
    Content-Type: text/plain
    
    foo=bar
    baz=The first line.
    The second line.
    Method: POST; Encoding type: multipart/form-data:
    
    Content-Type: multipart/form-data; boundary=---------------------------314911788813839
    
    -----------------------------314911788813839
    Content-Disposition: form-data; name="foo"
    
    bar
    -----------------------------314911788813839
    Content-Disposition: form-data; name="baz"
    
    The first line.
    The second line.
    
    -----------------------------314911788813839--
    However, if you are using the GET method, a string like the following will be simply added to the URL:
    
    ?foo=bar&baz=The%20first%20line.%0AThe%20second%20line.
    A little vanilla framework
    All these effects are done automatically by the web browser whenever you submit a <form>. If you want to perform the same effects using JavaScript you have to instruct the interpreter about everything. Therefore, how to send forms in pure AJAX is too complex to be explained here in detail. For this reason, here we place a complete (yet didactic) framework, able to use all four ways to submit, and to upload files:
    
    <!doctype html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Sending forms with pure AJAX &ndash; MDN</title>
    <script type="text/javascript">
    
    "use strict";
    
    /*\
    |*|
    |*|  :: XMLHttpRequest.prototype.sendAsBinary() Polyfill ::
    |*|
    |*|  https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#sendAsBinary()
    \*/
    
    if (!XMLHttpRequest.prototype.sendAsBinary) {
      XMLHttpRequest.prototype.sendAsBinary = function(sData) {
        var nBytes = sData.length, ui8Data = new Uint8Array(nBytes);
        for (var nIdx = 0; nIdx < nBytes; nIdx++) {
          ui8Data[nIdx] = sData.charCodeAt(nIdx) & 0xff;
        }
        /* send as ArrayBufferView...: */
        this.send(ui8Data);
        /* ...or as ArrayBuffer (legacy)...: this.send(ui8Data.buffer); */
      };
    }
    
    /*\
    |*|
    |*|  :: AJAX Form Submit Framework ::
    |*|
    |*|  https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest
    |*|
    |*|  This framework is released under the GNU Public License, version 3 or later.
    |*|  https://www.gnu.org/licenses/gpl-3.0-standalone.html
    |*|
    |*|  Syntax:
    |*|
    |*|   AJAXSubmit(HTMLFormElement);
    \*/
    
    var AJAXSubmit = (function () {
    
      function ajaxSuccess () {
        /* console.log("AJAXSubmit - Success!"); */
        console.log(this.responseText);
        /* you can get the serialized data through the "submittedData" custom property: */
        /* console.log(JSON.stringify(this.submittedData)); */
      }
    
      function submitData (oData) {
        /* the AJAX request... */
        var oAjaxReq = new XMLHttpRequest();
        oAjaxReq.submittedData = oData;
        oAjaxReq.onload = ajaxSuccess;
        if (oData.technique === 0) {
          /* method is GET */
          oAjaxReq.open("get", oData.receiver.replace(/(?:\?.*)?$/,
              oData.segments.length > 0 ? "?" + oData.segments.join("&") : ""), true);
          oAjaxReq.send(null);
        } else {
          /* method is POST */
          oAjaxReq.open("post", oData.receiver, true);
          if (oData.technique === 3) {
            /* enctype is multipart/form-data */
            var sBoundary = "---------------------------" + Date.now().toString(16);
            oAjaxReq.setRequestHeader("Content-Type", "multipart\/form-data; boundary=" + sBoundary);
            oAjaxReq.sendAsBinary("--" + sBoundary + "\r\n" +
                oData.segments.join("--" + sBoundary + "\r\n") + "--" + sBoundary + "--\r\n");
          } else {
            /* enctype is application/x-www-form-urlencoded or text/plain */
            oAjaxReq.setRequestHeader("Content-Type", oData.contentType);
            oAjaxReq.send(oData.segments.join(oData.technique === 2 ? "\r\n" : "&"));
          }
        }
      }
    
      function processStatus (oData) {
        if (oData.status > 0) { return; }
        /* the form is now totally serialized! do something before sending it to the server... */
        /* doSomething(oData); */
        /* console.log("AJAXSubmit - The form is now serialized. Submitting..."); */
        submitData (oData);
      }
    
      function pushSegment (oFREvt) {
        this.owner.segments[this.segmentIdx] += oFREvt.target.result + "\r\n";
        this.owner.status--;
        processStatus(this.owner);
      }
    
      function plainEscape (sText) {
        /* How should I treat a text/plain form encoding?
           What characters are not allowed? this is what I suppose...: */
        /* "4\3\7 - Einstein said E=mc2" ----> "4\\3\\7\ -\ Einstein\ said\ E\=mc2" */
        return sText.replace(/[\s\=\\]/g, "\\$&");
      }
    
      function SubmitRequest (oTarget) {
        var nFile, sFieldType, oField, oSegmReq, oFile, bIsPost = oTarget.method.toLowerCase() === "post";
        /* console.log("AJAXSubmit - Serializing form..."); */
        this.contentType = bIsPost && oTarget.enctype ? oTarget.enctype : "application\/x-www-form-urlencoded";
        this.technique = bIsPost ?
            this.contentType === "multipart\/form-data" ? 3 : this.contentType === "text\/plain" ? 2 : 1 : 0;
        this.receiver = oTarget.action;
        this.status = 0;
        this.segments = [];
        var fFilter = this.technique === 2 ? plainEscape : escape;
        for (var nItem = 0; nItem < oTarget.elements.length; nItem++) {
          oField = oTarget.elements[nItem];
          if (!oField.hasAttribute("name")) { continue; }
          sFieldType = oField.nodeName.toUpperCase() === "INPUT" ? oField.getAttribute("type").toUpperCase() : "TEXT";
          if (sFieldType === "FILE" && oField.files.length > 0) {
            if (this.technique === 3) {
              /* enctype is multipart/form-data */
              for (nFile = 0; nFile < oField.files.length; nFile++) {
                oFile = oField.files[nFile];
                oSegmReq = new FileReader();
                /* (custom properties:) */
                oSegmReq.segmentIdx = this.segments.length;
                oSegmReq.owner = this;
                /* (end of custom properties) */
                oSegmReq.onload = pushSegment;
                this.segments.push("Content-Disposition: form-data; name=\"" +
                    oField.name + "\"; filename=\"" + oFile.name +
                    "\"\r\nContent-Type: " + oFile.type + "\r\n\r\n");
                this.status++;
                oSegmReq.readAsBinaryString(oFile);
              }
            } else {
              /* enctype is application/x-www-form-urlencoded or text/plain or
                 method is GET: files will not be sent! */
              for (nFile = 0; nFile < oField.files.length;
                  this.segments.push(fFilter(oField.name) + "=" + fFilter(oField.files[nFile++].name)));
            }
          } else if ((sFieldType !== "RADIO" && sFieldType !== "CHECKBOX") || oField.checked) {
            /* NOTE: this will submit _all_ submit buttons. Detecting the correct one is non-trivial. */
            /* field type is not FILE or is FILE but is empty */
            this.segments.push(
              this.technique === 3 ? /* enctype is multipart/form-data */
                "Content-Disposition: form-data; name=\"" + oField.name + "\"\r\n\r\n" + oField.value + "\r\n"
              : /* enctype is application/x-www-form-urlencoded or text/plain or method is GET */
                fFilter(oField.name) + "=" + fFilter(oField.value)
            );
          }
        }
        processStatus(this);
      }
    
      return function (oFormElement) {
        if (!oFormElement.action) { return; }
        new SubmitRequest(oFormElement);
      };
    
    })();
    
    </script>
    </head>
    <body>
    
    <h1>Sending forms with pure AJAX</h1>
    
    <h2>Using the GET method</h2>
    
    <form action="register.php" method="get" onsubmit="AJAXSubmit(this); return false;">
      <fieldset>
        <legend>Registration example</legend>
        <p>
          First name: <input type="text" name="firstname" /><br />
          Last name: <input type="text" name="lastname" />
        </p>
        <p>
          <input type="submit" value="Submit" />
        </p>
      </fieldset>
    </form>
    
    <h2>Using the POST method</h2>
    <h3>Enctype: application/x-www-form-urlencoded (default)</h3>
    
    <form action="register.php" method="post" onsubmit="AJAXSubmit(this); return false;">
      <fieldset>
        <legend>Registration example</legend>
        <p>
          First name: <input type="text" name="firstname" /><br />
          Last name: <input type="text" name="lastname" />
        </p>
        <p>
          <input type="submit" value="Submit" />
        </p>
      </fieldset>
    </form>
    
    <h3>Enctype: text/plain</h3>
    
    <form action="register.php" method="post" enctype="text/plain"
        onsubmit="AJAXSubmit(this); return false;">
      <fieldset>
        <legend>Registration example</legend>
        <p>
          Your name: <input type="text" name="user" />
        </p>
        <p>
          Your message:<br />
          <textarea name="message" cols="40" rows="8"></textarea>
        </p>
        <p>
          <input type="submit" value="Submit" />
        </p>
      </fieldset>
    </form>
    
    <h3>Enctype: multipart/form-data</h3>
    
    <form action="register.php" method="post" enctype="multipart/form-data"
        onsubmit="AJAXSubmit(this); return false;">
      <fieldset>
        <legend>Upload example</legend>
        <p>
          First name: <input type="text" name="firstname" /><br />
          Last name: <input type="text" name="lastname" /><br />
          Sex:
          <input id="sex_male" type="radio" name="sex" value="male" />
          <label for="sex_male">Male</label>
          <input id="sex_female" type="radio" name="sex" value="female" />
          <label for="sex_female">Female</label><br />
          Password: <input type="password" name="secret" /><br />
          What do you prefer:
          <select name="image_type">
            <option>Books</option>
            <option>Cinema</option>
            <option>TV</option>
          </select>
        </p>
        <p>
          Post your photos:
          <input type="file" multiple name="photos[]">
        </p>
        <p>
          <input id="vehicle_bike" type="checkbox" name="vehicle[]" value="Bike" />
          <label for="vehicle_bike">I have a bike</label><br />
          <input id="vehicle_car" type="checkbox" name="vehicle[]" value="Car" />
          <label for="vehicle_car">I have a car</label>
        </p>
        <p>
          Describe yourself:<br />
          <textarea name="description" cols="50" rows="8"></textarea>
        </p>
        <p>
          <input type="submit" value="Submit" />
        </p>
      </fieldset>
    </form>
    
    </body>
    </html>
    To test this, create a page named register.php (which is the action attribute of these sample forms), and put the following minimalistic content:
    
    <?php
    /* register.php */
    
    header("Content-type: text/plain");
    
    /*
    NOTE: You should never use `print_r()` in production scripts, or
    otherwise output client-submitted data without sanitizing it first.
    Failing to sanitize can lead to cross-site scripting vulnerabilities.
    */
    
    echo ":: data received via GET ::\n\n";
    print_r($_GET);
    
    echo "\n\n:: Data received via POST ::\n\n";
    print_r($_POST);
    
    echo "\n\n:: Data received as \"raw\" (text/plain encoding) ::\n\n";
    if (isset($HTTP_RAW_POST_DATA)) { echo $HTTP_RAW_POST_DATA; }
    
    echo "\n\n:: Files received ::\n\n";
    print_r($_FILES);
    The syntax to activate this script is simply:
    
    AJAXSubmit(myForm);
    Note: This framework uses the FileReader API to transmit file uploads. This is a recent API and is not implemented in IE9 or below. For this reason, the AJAX-only upload is considered an experimental technique. If you do not need to upload binary files, this framework works fine in most browsers.
    Note: The best way to send binary content is via ArrayBuffers or Blobs in conjuncton with the send() method and possibly the readAsArrayBuffer() method of the FileReader API. But, since the aim of this script is to work with a stringifiable raw data, we used the sendAsBinary() method in conjunction with the readAsBinaryString() method of the FileReader API. As such, the above script makes sense only when you are dealing with small files. If you do not intend to upload binary content, consider instead using the FormData API.
    Note: The non-standard sendAsBinary method is considered deprecated as of Gecko 31 (Firefox 31 / Thunderbird 31 / SeaMonkey 2.28) and will be removed soon. The standard send(Blob data) method can be used instead.
    Using FormData objectsSection
    The FormData constructor lets you compile a set of key/value pairs to send using XMLHttpRequest. Its primary use is in sending form data, but can also be used independently from a form in order to transmit user keyed data. The transmitted data is in the same format the form's submit() method uses to send data, if the form's encoding type were set to "multipart/form-data". FormData objects can be utilized in a number of ways with an XMLHttpRequest. For examples, and explanations of how one can utilize FormData with XMLHttpRequests, see the Using FormData Objects page. For didactic purposes here is a translation of the previous example transformed to use the FormData API. Note the brevity of the code:
    
    <!doctype html>
    <html>
    <head>
    <meta http-equiv="Content-Type" charset="UTF-8" />
    <title>Sending forms with FormData &ndash; MDN</title>
    <script>
    "use strict";
    
    function ajaxSuccess () {
      console.log(this.responseText);
    }
    
    function AJAXSubmit (oFormElement) {
      if (!oFormElement.action) { return; }
      var oReq = new XMLHttpRequest();
      oReq.onload = ajaxSuccess;
      if (oFormElement.method.toLowerCase() === "post") {
        oReq.open("post", oFormElement.action);
        oReq.send(new FormData(oFormElement));
      } else {
        var oField, sFieldType, nFile, sSearch = "";
        for (var nItem = 0; nItem < oFormElement.elements.length; nItem++) {
          oField = oFormElement.elements[nItem];
          if (!oField.hasAttribute("name")) { continue; }
          sFieldType = oField.nodeName.toUpperCase() === "INPUT" ?
              oField.getAttribute("type").toUpperCase() : "TEXT";
          if (sFieldType === "FILE") {
            for (nFile = 0; nFile < oField.files.length;
                sSearch += "&" + escape(oField.name) + "=" + escape(oField.files[nFile++].name));
          } else if ((sFieldType !== "RADIO" && sFieldType !== "CHECKBOX") || oField.checked) {
            sSearch += "&" + escape(oField.name) + "=" + escape(oField.value);
          }
        }
        oReq.open("get", oFormElement.action.replace(/(?:\?.*)?$/, sSearch.replace(/^&/, "?")), true);
        oReq.send(null);
      }
    }
    </script>
    </head>
    <body>
    
    <h1>Sending forms with FormData</h1>
    
    <h2>Using the GET method</h2>
    
    <form action="register.php" method="get" onsubmit="AJAXSubmit(this); return false;">
      <fieldset>
        <legend>Registration example</legend>
        <p>
          First name: <input type="text" name="firstname" /><br />
          Last name: <input type="text" name="lastname" />
        </p>
        <p>
          <input type="submit" value="Submit" />
        </p>
      </fieldset>
    </form>
    
    <h2>Using the POST method</h2>
    <h3>Enctype: application/x-www-form-urlencoded (default)</h3>
    
    <form action="register.php" method="post" onsubmit="AJAXSubmit(this); return false;">
      <fieldset>
        <legend>Registration example</legend>
        <p>
          First name: <input type="text" name="firstname" /><br />
          Last name: <input type="text" name="lastname" />
        </p>
        <p>
          <input type="submit" value="Submit" />
        </p>
      </fieldset>
    </form>
    
    <h3>Enctype: text/plain</h3>
    
    <p>The text/plain encoding is not supported by the FormData API.</p>
    
    <h3>Enctype: multipart/form-data</h3>
    
    <form action="register.php" method="post" enctype="multipart/form-data"
        onsubmit="AJAXSubmit(this); return false;">
      <fieldset>
        <legend>Upload example</legend>
        <p>
          First name: <input type="text" name="firstname" /><br />
          Last name: <input type="text" name="lastname" /><br />
          Sex:
          <input id="sex_male" type="radio" name="sex" value="male" />
          <label for="sex_male">Male</label>
          <input id="sex_female" type="radio" name="sex" value="female" />
          <label for="sex_female">Female</label><br />
          Password: <input type="password" name="secret" /><br />
          What do you prefer:
          <select name="image_type">
            <option>Books</option>
            <option>Cinema</option>
            <option>TV</option>
          </select>
        </p>
        <p>
          Post your photos:
          <input type="file" multiple name="photos[]">
        </p>
        <p>
          <input id="vehicle_bike" type="checkbox" name="vehicle[]" value="Bike" />
          <label for="vehicle_bike">I have a bike</label><br />
          <input id="vehicle_car" type="checkbox" name="vehicle[]" value="Car" />
          <label for="vehicle_car">I have a car</label>
        </p>
        <p>
          Describe yourself:<br />
          <textarea name="description" cols="50" rows="8"></textarea>
        </p>
        <p>
          <input type="submit" value="Submit" />
        </p>
      </fieldset>
    </form>
    </body>
    </html>
    Note: As we said, FormData objects are not stringifiable objects. If you want to stringify a submitted data, use the previous pure-AJAX example. Note also that, although in this example there are some file <input> fields, when you submit a form through the FormData API you do not need to use the FileReader API also: files are automatically loaded and uploaded.

  • 相关阅读:
    UltraWebGrid多表头
    2009个人年度总结报告(IT)
    DevExpress分发
    AspxTreeList数据绑定以及模板和外观定制的运用
    每日一句英语:No problem, Mr. Smith. Anything else?
    “向程序发送命令时出现问题”的解决方法
    ASP常用进制转化类(2,8,10,16,32,64)
    我的分页用户控件(性能问题)
    研发的那些事2—设计之惑
    一个架构的演化2用ESB集成
  • 原文地址:https://www.cnblogs.com/ioriwellings/p/11427851.html
Copyright © 2020-2023  润新知