1

When send a file that has a Vendor Specific Content-Type to an application ASP.NET Core 1.0 your Content-Type is transformed into a different content type.

Question: How to keep the original Content-Type in upload/storage (Azure)/download. Should any changes be made to Startup.cs orWeb.config (done manually, but does it work)?

Content-Type Original: application/vnd.ms-pki.stl

Content-Type after send or autobind (I did not identify when the conversion is done): application/octet-stream

Submission: the form is sent via Ajax

$.ajax({
        url: url,
        data: new FormData($("#")[0]),
        type: 'POST',
        processData: false,
        contentType: false,
        beforeSend: function () {
            // omitted
        },
        success: function (data) {
            // omitted
        },
        error: function (data) {
            // omitted
        }
   });

Target Action:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create(PrintObjectCreateVM printObject)
    {

            if (!ModelState.IsValid)
            {
                return NotFound();
            }

            await PrintObjectService.Insert(printObject);

            return Ok();
    }

Action Model:

public class PrintObjectCreateVM
{
    public IFormFile PrintObjectImage { get; set; }

    public IFormFile PrintObjectPrintFile { get; set; }
}
Ricardo
  • 677
  • 4
  • 11
  • 35

1 Answers1

1

When the browser is unable to determine the content-type of the file to be uploaded (e.g., inferred from the file extension or operating system typing information) it defaults to application/octet-stream

For downloading, manually set the Content-Type when returning the file. If the framework cannot identify the content it defaults to application/octet-stream which any client should be able to handler.

For example

[Route("api/[controller]")]
public class DownloadController : Controller {
    //GET api/download/12345abc
    [HttpGet("{id}"]
    public async Task<IActionResult> Download(string id) {
        var stream = await {{__get_stream_here__}}
        var response = File(stream, "application/vnd.ms-pki.stl"); // FileStreamResult
        return response;
    }    
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Maybe work to download but on upload I need check if the content-type is accepted to the aplication. – Ricardo Jun 24 '17 at 17:45
  • 1
    @Ricardo I am still searching, but take a look at this for the time being. https://stackoverflow.com/a/26303098/5233410 What seems to be happening is that it looks like the browser uploading the file can't identify the custom vendor mime type of the file. – Nkosi Jun 24 '17 at 18:33
  • 1
    @Ricardo found something that looks promising. Try setting the type on the input tag eg `` reference [this](https://www.w3schools.com/tags/att_input_accept.asp) for more on how to set it – Nkosi Jun 24 '17 at 19:03
  • @Ricardo, I created a small project and tested it with a known and unknown file type. When using known type content-type was properly set by the browser and unknown's content-type was set to `application/octet-stream`. This would indicate that it is dependent on the client which would be outside of your control. – Nkosi Jun 24 '17 at 19:47
  • I understand, so I'll use this plugin https://jqueryvalidation.org/extension-method/ to validate the format by the browser before sending it to the server, Thank you! – Ricardo Jun 24 '17 at 19:52
  • @Ricardo no problem. glad to help. Happy coding. – Nkosi Jun 24 '17 at 19:54