如何将文件与参数一起发布到webapi方法?

时间:2015-07-13 04:53:07

标签: asp.net-web-api content-type fiddler

我是ASP.NET Web API的新手。我有一个示例FileUpload web api(来自某个站点)将文件上传到服务器。 以下工作正常上传文件。

public async Task<HttpResponseMessage> FileUpload()
       {
           // Check whether the POST operation is MultiPart?
           if (!Request.Content.IsMimeMultipartContent())
           {
               throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
           }

           // Prepare CustomMultipartFormDataStreamProvider in which our multipart form
           // data will be loaded.
           //string fileSaveLocation = HttpContext.Current.Server.MapPath("~/App_Data");
           string fileSaveLocation = HttpContext.Current.Server.MapPath("~/UploadedFiles");
           CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(fileSaveLocation);
           List<string> files = new List<string>();

           try
           {
               // Read all contents of multipart message into CustomMultipartFormDataStreamProvider.
               await Request.Content.ReadAsMultipartAsync(provider);

               foreach (MultipartFileData file in provider.FileData)
               {
                   files.Add(Path.GetFileName(file.LocalFileName));
               }

               // Send OK Response along with saved file names to the client.
               return Request.CreateResponse(HttpStatusCode.OK, files);
           }
           catch (System.Exception e)
           {
               return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
           }
       }
       // We implement MultipartFormDataStreamProvider to override the filename of File which
       // will be stored on server, or else the default name will be of the format like Body-
       // Part_{GUID}. In the following implementation we simply get the FileName from 
       // ContentDisposition Header of the Request Body.
       public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
       {
           public CustomMultipartFormDataStreamProvider(string path) : base(path) { }

           public override string GetLocalFileName(HttpContentHeaders headers)
           {
               return headers.ContentDisposition.FileName.Replace("\"", string.Empty);
           }
       }

但是,我想使用parameter将以'token'类型为string的{​​{1}} [FromBody]发送到以下方法?

必需:

 public async Task<HttpResponseMessage> FileUpload([FromBody] string token)
    {
    //somecode here
    }

那么,基本上我们可以将multiple Content-Type数据发送到网络API吗?请建议。我正在使用Fiddler来测试webapi。

例如:

Request Body(json): { “标记”: “FV00VYAP”}

1 个答案:

答案 0 :(得分:1)

您可以通过查询字符串传递额外的内容,然后从CustomMultipartFormDataStreamProvider中读取它。

// Read all contents of multipart message into CustomMultipartFormDataStreamProvider.
               await Request.Content.ReadAsMultipartAsync(provider);

然后您可以使用provider.FormData来读取您传递的额外值。

// 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));
            }
        }

有关详细信息,请参阅http://www.asp.net/web-api/overview/advanced/sending-html-form-data,-part-2