• 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>

  • 相关阅读:
    哈工大中文篇章关系语料
    MongoDB学习笔记~关于官方驱动集成IQueryable之后的一些事
    MongoDB学习笔记~为IMongoRepository接口更新指定字段
    MongoDB学习笔记系列
    MongoDB学习笔记~为IMongoRepository接口添加了增删改方法,针对官方驱动
    MongoDB学习笔记~为IMongoRepository接口添加了排序和表达式树,针对官方驱动
    Android NDK入门实例 计算斐波那契数列二生成.so库文件
    Spring Autowire自动装配
    在gem5的full system下运行 alpha编译的测试程序 running gem5 on ubuntu in full system mode in alpha
    工厂三兄弟之抽象工厂模式(二)
  • 原文地址:https://www.cnblogs.com/fx2008/p/3301447.html
Copyright © 2020-2023  润新知