zoukankan      html  css  js  c++  java
  • Sending HTML Form Data

    public Task<HttpResponseMessage> PostFormData()
    {
    // Check if the request contains multipart/form-data.
    if (!Request.Content.IsMimeMultipartContent())
    {
    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
    }

    string root = HttpContext.Current.Server.MapPath("~/App_Data");
    var provider = new MultipartFormDataStreamProvider(root);

    // Read the form data and return an async task.
    var task = Request.Content.ReadAsMultipartAsync(provider).
    ContinueWith<HttpResponseMessage>(t =>
    {
    if (t.IsFaulted || t.IsCanceled)
    {
    Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
    }

    // This illustrates how to get the file names.
    foreach (MultipartFileData file in provider.FileData)
    {
    Trace.WriteLine(file.Headers.ContentDisposition.FileName);
    Trace.WriteLine("Server file path: " + file.LocalFileName);
    }
    return Request.CreateResponse(HttpStatusCode.OK);
    });

    return task;
    }
    Reading Form Control Data

    The HTML form that I showed earlier had a text input control.

    <div>
    <label for="caption">Image Caption</label>
    <input name="caption" type="text" />
    </div>
    You can get the value of the control from the FormData property of the MultipartFormDataStreamProvider.

    public async Task<HttpResponseMessage> PostFormData()
    {
    if (!Request.Content.IsMimeMultipartContent())
    {
    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
    }

    string root = HttpContext.Current.Server.MapPath("~/App_Data");
    var provider = new MultipartFormDataStreamProvider(root);

    try
    {
    await Request.Content.ReadAsMultipartAsync(provider);

    // Show all the key-value pairs.
    foreach (var key in provider.FormData.AllKeys)
    {
    foreach (var val in provider.FormData.GetValues(key))
    {
    Trace.WriteLine(string.Format("{0}: {1}", key, val));
    }
    }

    return Request.CreateResponse(HttpStatusCode.OK);
    }
    catch (System.Exception e)
    {
    return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
    }
    }
    FormData is a NameValueCollection that contains name/value pairs for the form controls. The collection can contain duplicate keys. Consider this form:

    <form name="trip_search" method="post" enctype="multipart/form-data" action="/www.asp.net/web-api/overview/working-with-http/api/upload">
    <div>
    <input type="radio" name="trip" value="round-trip"/>
    Round-Trip
    </div>
    <div>
    <input type="radio" name="trip" value="one-way"/>
    One-Way
    </div>

    <div>
    <input type="checkbox" name="options" value="nonstop" />
    Only show non-stop flights
    </div>
    <div>
    <input type="checkbox" name="options" value="airports" />
    Compare nearby airports
    </div>
    <div>
    <input type="checkbox" name="options" value="dates" />
    My travel dates are flexible
    </div>

    <div>
    <label for="seat">Seating Preference</label>
    <select name="seat">
    <option value="aisle">Aisle</option>
    <option value="window">Window</option>
    <option value="center">Center</option>
    <option value="none">No Preference</option>
    </select>
    </div>
    </form>

  • 相关阅读:
    教程-Supports判断接口(Instance)是否支持
    Android实例-如何使用系统剪切板(XE8+小米2)
    Android实例-从照相机或图库获取照片(XE8+小米2)
    Android实例-TTabControl的使用(XE8+小米2)
    Android实例-闪光灯的控制(XE8+小米2)
    WCF小问题总汇
    wpf程序线程与UI内容交互
    xml解析原理一些想法
    XML序列化
    C#用正则表达式一键Unicode转UTF8(解决LitJson中文问题)
  • 原文地址:https://www.cnblogs.com/fx2008/p/3301447.html
Copyright © 2011-2022 走看看