Downloading a PDF file from Fiddler to the WebAPI method results in 415 unsupported media type

I am trying to load a PDF file into my WebAPI method, but I am getting 415 unsupported media type. Do I need to put something specific in my WebApiConfig.cs file to enable PDF formatting?

Here is the signature for my controller code:

[HttpPost]
        public HttpResponseMessage SavePdf(HttpPostedFileBase file)
        {
            string fileLocation = @"\\server\shared\appname\";
            ...

My WebAPI is currently hosted on localhost. I call this method WebAPI from Fiddler. Here's the Composer tab of Fiddler (then I click on the “Blue Upload File” link to download my PDF file):

enter image description here

This is where HTTP 415 error occurs in Fiddler:

enter image description here

It seems to me that I need to somehow say that my WebAPI accepts the type of PDF media file (application / pdf), but I'm not sure ... any ideas?

+4
2

, , global.asax:

var config = System.Web.Http.GlobalConfiguration.Configuration;
config.Formatters.Insert(0, new System.Net.Http.Formatting.JsonMediaTypeFormatter());

2 : - :

public class UploadController : ApiController
{
    public async Task < HttpResponseMessage > PostFormData() 
    {
        var root = HttpContext.Current.Server.MapPath("~/App_Data");
        var provider = new MultipartFormDataStreamProvider(root);

        try 
        {
            // Read the form data.
            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)) 
                {
                    var keyValue = string.Format("{0}: {1}", key, val);
                }
            }

            foreach(MultipartFileData fileData in provider.FileData) 
            {
                var fileName = fileData.Headers.ContentDisposition.FileName;
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        } 
        catch (Exception e) 
        {
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
        }
    }
}
...

:

+3

:
http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2

HttpPostedFileBase , -API , ... -API multipart ... ... HttpPostedFileBase MVC.

, file.Headers.ContentDisposition.FileName .

+3

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


All Articles