Generate file upload input for property with “DataType.Upload” attribute?

后端 未结 3 1345
野趣味
野趣味 2021-01-07 05:50

I have the following view model.

public class MyViewModel
{
    [DataType(DataType.Upload)]
    public HttpPostedFileBase ImageUpload { get; set; }

    publ         


        
3条回答
  •  孤街浪徒
    2021-01-07 06:18

    I have another idea. How about getting rid of the HttpPostedFileBase from the model and from the controller either ?

    In your html, you would add:

     @using (Html.BeginForm("actionNameYouWriteToManupulate", "controllerYouWrite", FormMethod.Post, new { enctype = "multipart/form-data" }))
     {
       
       
       ...
       
     }
    

    Add Files as many files you want, and in the controller :

    var hfc = Request.Files;
    for (int i = 0; i < hfc.Count; i++)
                {
                    var hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
                       //Do what you want with files 
                       //Handler pointed to file is hpf for example :
                       hpf.SaveAs(Server.MapPath(fullPathWithFileName));
                     }
                }
    

    Hope to be usefull.

提交回复
热议问题