Неподдерживаемый ответ типа HTTP файла при загрузке файла с помощью С# api. - программирование
Подтвердить что ты не робот

Неподдерживаемый ответ типа HTTP файла при загрузке файла с помощью С# api.

Я использую директиву angular и oi.file.js https://github.com/tamtakoe/oi.file

Мой html выглядит так:

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

Angular контроллер:

            $scope.file = {};

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

И контроллер С# api:

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

Когда я пытаюсь загрузить файл, я получаю ответ типа Unsupported Media Type.

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: */*
4b9b3361

Ответ 1

Спокойно... Я не знаю, что произойдет. Но мой метод запускается после удаления параметра из метода.

public HttpResponseMessage Post() 

Таким образом, метод прекрасных работ для загрузки файлов с использованием контроллера С# api:

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

Ответ 2

В дополнение к k.makarov answer, Удаление параметра пропускает проверку типа многочастного содержимого во время публикации. См. Эту статью - Отправка многочастных данных. Данные файла и формы вместе

В этой строке в статье конкретно говорится: "Обратите внимание, что действие контроллера не принимает никаких параметров. Это потому, что мы обрабатываем тело запроса внутри действия, не вызывая форматтера медиа-типа".