Я использую директиву angular и oi.file.js из https://github.com/tamtakoe/oi.file
Мой html выглядит так:< /p>
Контроллер Angular:
$scope.file = {};
$scope.options = {
change: function (file) {
console.log($scope.file);
file.$upload('api/fileupload', $scope.file);
console.log($scope.file);
}
};
И контроллер API C#:
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 logHelper;
private readonly SqlConnectionFactory sqlConnectionFactory;
public FileUploadController()
{
logHelper = new LogHelper();
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();
using (var connection = sqlConnectionFactory.Create())
using (var transaction = connection.BeginTransaction())
{
for (var i = 0; i < request.Files.Count; i++)
{
var postedFile = request.Files;
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; }
}
}
}
Когда я пытаюсь загрузить файл, я получаю ответ «Неподдерживаемый тип носителя».
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: */*
Подробнее здесь: https://stackoverflow.com/questions/212 ... -sharp-api
Неподдерживаемый HTTP-ответ типа носителя при загрузке файла с помощью API C#. ⇐ C#
Место общения программистов C#
1729021997
Anonymous
Я использую директиву angular и oi.file.js из https://github.com/tamtakoe/oi.file
Мой html выглядит так:< /p>
Контроллер Angular:
$scope.file = {};
$scope.options = {
change: function (file) {
console.log($scope.file);
file.$upload('api/fileupload', $scope.file);
console.log($scope.file);
}
};
И контроллер API C#:
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 logHelper;
private readonly SqlConnectionFactory sqlConnectionFactory;
public FileUploadController()
{
logHelper = new LogHelper();
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();
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; }
}
}
}
Когда я пытаюсь загрузить файл, я получаю ответ «Неподдерживаемый тип носителя».
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: */*
Подробнее здесь: [url]https://stackoverflow.com/questions/21257838/unsupported-media-type-http-response-when-upload-file-using-c-sharp-api[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия