1

In MVC zu schreiben, ich HttpPostedFileBase verwenden Datei von Form zu erhalten (multipart/form-data):Wie Listendatei von MVC Web API

Ausblick:

<form role="form" enctype="multipart/form-data"> 
    <input type="text" name="ProductCode"/> 
    <input type="text" name="ProductCat"/> 
    <input type="text" name="Price"/> 
    <input type="text" name="file" multiple/> 
</form> 

Modell:

public class Product 
{ 
    public int ProductId{ set; get; } 
    public string ProductCode { set; get; } 
    public int ProductCat { set; get; } 
    public decimal Price { set; get; } 
} 
public class ProductData 
{ 
    public Product pro { set; get; } 
    public List<HttpPostedFileBase> file { set; get; } 
} 

Aktion:

public ActionResult PostProduct(Product prop,List<HttpPostedFileBase> file) 
{ 
    ProductData data = new ProductData(); 
    data.pro=prop; 
    data.file=file; 
    client.BaseAddress = new Uri("http://localhost/99");  
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
    HttpResponseMessage res = client.PostAsJsonAsync("/api/product/postproduct",data).Result; 
    // in here , i get error :"error getting value from 'readtimeout' on 'system.web.httpinputstream'",and i cannot post data to web API 
    if (res.IsSuccessStatusCode) 
    { 
     return PartialView("_Success"); 
    } 
    else 
     return PartialView("_Error"); 
    } 

In Web-API:

[Route("postproduct")] 
[ResponseType(typeof(Product))] 
public IHttpActionResult PostProduct(ProductData prod) 
{ 
    .... 
} 

Ich weiß nicht, wie die Daten schreiben (Modell-Datei) von MVC Web-API?

+0

Ist das Web api Endpunkt Teil derselben Website? Wenn ja, warum müssen Sie es per HTTP posten? Sie können den Code innerhalb des Web-API-Endpunkts für eine allgemeine Klasse beibehalten und die Methode von Ihrer Mvc-Aktionsmethode aufrufen. – Shyju

+0

Ich weiß, aber ich möchte Web-APi für Multi-Plattform verwenden. Ich möchte die Datei speichern, um sie für alle Plattformen mit Web-API zu teilen. – Math9

Antwort

0

Es beinhaltet ein wenig Zeremonie.

Dieser Artikel sollte die Grundlagen zum Hochladen einer Datei mit Formularparametern http://www.asp.net/web-api/overview/advanced/sending-html-form-data-part-2 kümmern.

Aus Gründen der Klarheit ein Beispiel aus dem Artikel:

public async Task<HttpResponseMessage> PostFormData() 
{ 
    // 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); 

    try 
    { 
     // 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); 
     } 

     // Show all the key-value pairs. 
     foreach (var key in provider.FormData.AllKeys) 
     { 
      foreach (var val in provider.FormData.GetValues(key)) 
      { 
       Trace.WriteLine(string.Format("{0}: {1}", key, val)); 
      } 
     } 

     return Request.CreateResponse(HttpStatusCode.OK); 
    } 
    catch (System.Exception e) 
    { 
     return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); 
    } 
} 
Verwandte Themen