Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsupported Media Type http response when upload file using c# api.

I am using angular and oi.file.js directive from https://github.com/tamtakoe/oi.file

My html looks like this:

<form name="EditOrder" enctype="multipart/form-data">
  <input type="file" oi-file="options">
</form>

Angular controller:

            $scope.file = {};

            $scope.options = {
                change: function (file) {
                    console.log($scope.file);
                    file.$upload('api/fileupload', $scope.file);
                    console.log($scope.file);
                }
            };

And C# api controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Http;
using Ttmc.Core.Database;
using Ttmc.Core.Files;
using Ttmc.WebApp.Common;

namespace Ttmc.WebApp.Controllers
{
    public class FileUploadController: ApiController
    {
        private readonly FileRepository fileRepository;
        private readonly LogHelper<FileController> logHelper;
        private readonly SqlConnectionFactory sqlConnectionFactory;

        public FileUploadController()
        {
            logHelper = new LogHelper<FileController>();
            sqlConnectionFactory = new SqlConnectionFactory();
            fileRepository = new FileRepository(new DatabaseFileRepository(sqlConnectionFactory), sqlConnectionFactory);
        }

        [HttpPost]
        public HttpResponseMessage Post(FileController.PostFile postFile)
        {
            var request = HttpContext.Current.Request;
            HttpResponseMessage result = null;
            logHelper.LogExecute(() =>
            {
                if (request.Files.Count == 0)
                {
                    result = Request.CreateResponse(HttpStatusCode.BadRequest);
                }
                var resultFiles = new List<DatabaseFile>();
                using (var connection = sqlConnectionFactory.Create())
                using (var transaction = connection.BeginTransaction())
                {
                    for (var i = 0; i < request.Files.Count; i++)
                    {
                        var postedFile = request.Files[i];
                        var id = fileRepository.AddFile(postedFile.InputStream, postedFile.FileName, postFile.OrderId,
                                                        postFile.RootFolderName, connection, transaction);
                        resultFiles.Add(fileRepository.GetInfo(id, connection, transaction));
                    }
                    transaction.Commit();
                }
                result = Request.CreateResponse(HttpStatusCode.Created, resultFiles);
            });

            return result;
        }

        public class PostFile
        {
            public Guid OrderId { get; set; }
            public string RootFolderName { get; set; }
        }
    }
}

When I try to upload file I get Unsupported Media Type response.

POST /api/fileupload HTTP/1.1
Host: localhost:12345
Connection: keep-alive
Content-Length: 283
Cache-Control: no-cache
Pragma: no-cache
Origin: http://localhost:12345
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)   Chrome/32.0.1700.76 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary6wBPmLUA5JPOBZYm
Accept: */*
like image 754
k.makarov Avatar asked Jan 21 '14 12:01

k.makarov


1 Answers

Quite simple... I don't know what happened, but my method started to be invoked after I removed its parameter.

public HttpResponseMessage Post() 

So the working method for file upload using C# API controller is:

[HttpPost]
    public HttpResponseMessage Post()
    {
        var request = HttpContext.Current.Request;
        HttpResponseMessage result = null;
        logHelper.LogExecute(() =>
        {
            if (request.Files.Count == 0)
            {
                result = Request.CreateResponse(HttpStatusCode.BadRequest);
            }
            var resultFiles = new List<DatabaseFile>();
            using (var connection = sqlConnectionFactory.Create())
            using (var transaction = connection.BeginTransaction())
            {
                for (var i = 0; i < request.Files.Count; i++)
                {
                    var postedFile = request.Files[i];
                    var id = fileRepository.AddFile(postedFile.InputStream, postedFile.FileName, postFile.OrderId,
                                                    postFile.RootFolderName, connection, transaction);
                    resultFiles.Add(fileRepository.GetInfo(id, connection, transaction));
                }
                transaction.Commit();
            }
            result = Request.CreateResponse(HttpStatusCode.Created, resultFiles);
        });

        return result;
    }
like image 61
k.makarov Avatar answered Oct 19 '22 06:10

k.makarov