Tôi có phương pháp ASP.NET Web API này và tôi muốn đăng một đối tượng và trong cùng một thời điểm, một tệp!Gửi biểu mẫu dữ liệu VÀ một tệp tới ASP.NET Web API
public async Task<IHttpActionResult> Post(Facility facility)
{
if (!ModelState.IsValid)
return BadRequest();
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider);
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
Trace.WriteLine(file.Headers.ContentDisposition.FileName);
Trace.WriteLine("Server file path: " + file.LocalFileName);
}
// Logic
// Login
return Ok(facilityManager.Insert(facility));
}
Và tôi muốn gọi nó, vì vậy tôi gửi yêu cầu này http với cáy:
Tiêu đề:
Content-Type: multipart/form-data; boundary=-------------------------acebdf13572468
User-Agent: Fiddler
Host: localhost:44301
Content-Length: 3279
Body:
---------------------------acebdf13572468
Content-Disposition: form-data; name="fieldNameHere"; filename="credits.txt"
Content-Type: text/plain
<@INCLUDE *C:\Program Files (x86)\Fiddler2\credits.txt*@>
---------------------------acebdf13572468
Content-Disposition: form-data; name="facility"
Content-Type: application/json
{
"FacilityTypeId":"1"
}
---------------------------acebdf13572468--
tôi nhận được một lỗi 415 mã có văn bản trả lời:
{"nội dung": "Loại đa phương tiện của yêu cầu 'multipart/form-data' không được hỗ trợ cho tài nguyên này.", "ExceptionMessage": "Không có MediaTypeFormatter nào có sẵn để đọc đối tượng kiểu 'Cơ sở' từ nội dung với loại phương tiện truyền thông 'multipart/form-data'. "," exceptionType ":" System.Net.Http.UnsupportedMediaTypeException "," stackTrace ":" tại System.Net.Http.HttpContentExtensions.ReadAsAsync [T] (Nội dung HttpContent, Type loại, IEnumerable
1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable
1 trình định dạng, IFormatterLogger formatterLogger, hủy bỏToken hủy bỏToken) \ r \ n tại System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync (HttpRequestMessage request, loại loại, IEnumerable`1 định dạng, IFormatterLogger formatterLogger, CancellationToken cancelToken) "}
Tôi đã tìm kiếm rất nhiều trước khi hỏi nhưng không tìm được giải pháp nào. Thnaks giúp đỡ.
EDIT:
Một lưu ý: Nếu tôi loại bỏ các "cơ sở" tham số, và để cho phương pháp này chỉ để tải lên các tập tin, nó hoạt động hoàn hảo, nhưng tôi muốn gửi JSON và một tập tin với nhau.
Xin chào, bạn có giải pháp cho vấn đề này không? Tôi đang gặp vấn đề tương tự. – uikrosoft
@uikrosoft, Không đặt đối số (tham số) trong hàm api (Làm cho nó không có tham số). Và nhận các giá trị tham số của bạn bên trong hàm đó từ nội dung bài đăng. Điều đó làm việc cho tôi. – Hashem
Đây là hướng dẫn đầy đủ: http://www.asp.net/web-api/overview/advanced/sending-html-form-data,-part-2 –