我是ASP.NET Web API
的新手。我有一个示例FileUpload
web 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);
}
}
帮助感谢!
答案 0 :(得分:1)
转到小提琴手,选择帖子类型,并提供本地Web API网址。
在请求主体中,只需上传文件并执行,它将直接转到webapi方法。
控制器方法应为[HttpPost]