2015-09-25 21 views
8

Bir REST denetleyicisine MultiPartFormData olarak bir HTTP isteği göndermem gerekiyor. Çalışıyordu, ama şimdi denetleyicimde bulunan onay, isteğin doğru türde olduğunu, hata ayıklayıcısında görebildiğimde bile doğru türden olmadığını iddia ediyor. Başvuru için:HttpRequest.Content.IsMimeMultipartContent(), true olması gerektiğinde false döndürüyor

using System; 
using System.IO; 
using System.Net.Http; 
using System.Net.Http.Headers; 
using System.Text; 

namespace QuickUploadTestHarness 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      using (var client = new HttpClient()) 
      using (var content = new MultipartFormDataContent()) 
      { 
       // Make sure to change API address 
       client.BaseAddress = new Uri("http://localhost"); 

       // Add first file content 
       var fileContent1 = new ByteArrayContent(File.ReadAllBytes(@"C:\<filepath>\test.txt")); 
       fileContent1.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") 
       { 
        FileName = "testData.txt" 
       }; 

       //Add Second file content 
       var fileContent2 = new ByteArrayContent(File.ReadAllBytes(@"C:\<filepath>\test.txt")); 
       fileContent2.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") 
       { 
        FileName = "Sample.txt" 
       }; 

       content.Add(fileContent1); 
       content.Add(fileContent2); 

       // Make a call to Web API 
       var result = client.PostAsync("/secret/endpoint/relevant/bits/here/", content).Result; 

       Console.WriteLine(result.StatusCode); 
       Console.ReadLine(); 
      } 
     } 
    } 
} 

Nasıl değil MultiPartFormData olarak yorumlanır ediliyor olması mümkündür:

enter image description here

İşte aradığını konsol uygulaması kodu? İsteğe bir name ve filename argüman alır content.Add aşırı kullanmayı deneyebilirsiniz MultiPartFormDataContent için

cevap

5

için "MultiPartFormDataContent kullanılarak " unutmayın. MSDN MultipartFormDataContent.Add Method (HttpContent, String, String)

Selamlar

+0

Bu çalıştı! Teşekkür ederim! – Matt

+0

Duymak güzeldi! :) –

+0

Üzgünüz ... Anlayamıyorum. Denetleyicimde IsMimeMultipartContent() öğesinin dönüşüne ne değiştirmem gerektiğini söyler misiniz? Dosyayı web istemcisinden, asp değil ve webapi denetleyicimden alıyorum. –

İlgili konular