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?
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
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