【发布时间】:2015-07-10 11:09:17
【问题描述】:
我是ASP.NET Web API 的新手。我有一个示例FileUploadweb api(来自某个站点)将文件上传到服务器。 但是,不知道如何使用 Fiddler 进行测试。
http://localhost:54208/myapi/api/webapi/FileUpload
在 test.aspx 页面上:以下工作正常。 我想知道如何通过 Fiddler 使用这个 API?
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="http://localhost:54208/myapi/api/webapi/FileUpload" id="ajaxUploadForm" novalidate="novalidate">
<fieldset>
<legend>Upload Form</legend>
<ol>
<li>
<label>Description </label>
<input type="text" style="width:317px" name="description" id="description">
</li>
<li>
<label>upload </label>
<input type="file" id="fileInput" name="fileInput" multiple>
</li>
<li>
<input type="submit" value="Upload" id="ajaxUploadButton" class="btn">
</li>
</ol>
</fieldset>
</form>
</body>
</html>
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);
}
}
帮助赞赏!
【问题讨论】:
-
最简单的方法是捕获一次运行页面的流量,然后将该会话从 Fiddler 的 Web 会话列表拖放到 COMPOSER 选项卡并根据需要进行修改。
-
好的,我会尝试...!还有一件事..是否可以在发布文件的同时将参数传递给他的方法?作为 public void ([FromBody] string token) 之类的?
-
FromBody 表示您在帖子正文中具有命名参数的值。
标签: file-upload asp.net-web-api fiddler