How to send a file using Fiddler to WebAPI using C #?

I'm new to ASP.NET Web API. I have a sample FileUpload web api(from some site) for uploading files to the server. But, I don’t know how to test it with Fiddler.

http: // localhost: 54208 / myapi / api / webapi / FileUpload

On the test.aspx page: Works fine. I want to know how to use this API using Fiddler?

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

Help rate!

0
source share
1 answer

Go to fiddler, select the type of entry, specify the local web address of the API. In the request body, just upload the file and execute it, it will go directly to the webapi method. The controller method must be[HttpPost]

+1

Source: https://habr.com/ru/post/1525816/


All Articles