How to upload files in MVC C # on the cell phone?

0

I am making a form in which you have to take a picture and answer on a cell phone. I have researched the best thing that I found is to use HttpPostedFileBase but I get the null parameter on the Internet I have seen examples with only the image but I also need to send a class.

Controller Code

[HttpPost]
    public ActionResult RecorrerAuditoria([Bind(Include = "Id,Answer,Comment,Assessment")] Audit_Question audit, HttpPostedFileBase file)
    {

        if (file != null && file.ContentLength > 0)
            try
            {
                string path = Path.Combine(@"\emxlocal01\LPA\Prueba Luis\Pictures\Audit", Path.GetFileName(file.FileName));
                file.SaveAs(path);
                ViewBag.Message = "File uploaded successfully";
                if (audit.Answer.Equals("Success"))
                {
                    audit.Status = "Closed";
                    audit.DateFinish = DateTime.Now;
                    audit.EmailSent = "noRequired";

                }
                else
                {
                    audit.Status = "failure";
                    audit.EmailSent = "Pending";
                }
                audit.PictureR.SrcPic = path;
                new Conn().RealizarQuestion(audit);
                return View(SessionHelper.CurrentQuestion.FirstOrDefault());
            }
            catch (Exception ex)
            {
                ViewBag.Message = "ERROR:" + ex.Message.ToString();
            }
        else
            ViewBag.Message = "You have not specified a file.";
        return View();
    }

View code

using (Html.BeginForm("RecorrerAuditoria", "Auditoria", FormMethod.Post, new 
{ enctype = "multipart/form-data"  }))
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <div class="row form-group">
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="col col-md-8"><h5 class="text-sm-center mt-2 mb-1">@Html.DisplayFor(a => a.rQuestion.Type)</h5></div>
        </div>
        <div class="row form-group">
            <div class="col col-md-15">
                @Html.DisplayFor(a => a.rQuestion.Question)
            </div>
        </div>
        <div class="row form-group">
            <div class="col col-md-9">
                <div class="form-check-inline form-check">
                    @Html.RadioButtonFor(model => model.Answer, "Success", new { id = "Success", @class = "form-check-input" })
                    @Html.Label("Conforme", "Conforme")

                    @Html.RadioButtonFor(model => model.Answer, "Failure", new { id = "Failure", @class = "form-check-input" })
                    @Html.Label("No Conforme", "NoConforme")
                </div>
            </div>
        </div>
        <div class="row form-group">
            <div class="col col-md-3"><label for="textarea-input" class="form-control-label">Observaciones</label></div>
            <div class="col-12 col-md-9">@Html.TextAreaFor(model => model.Comment, new { @class = "form-control", placeholder = "Contenido...", @rows = "4", @name = "textarea-input", @id = "textarea-input" })</div>
        </div>
        <div class="row form-group">
            <div class="col col-md-3"><label>foto verificacion:</label></div>
            <input type="file" accept="image/*" capture="camera">
        </div>
        <div id="Groupfind" class="row form-group">
            <div class="col col-md-3"><label>foto de falla:</label></div>
            <input type="file" accept="image/*" capture="camera">
        </div>
        <div class="row form-group">
            <div class="col col-md-3"><label class="form-control-label">Calif Auditor</label></div>
            <div class="col col-md-9">
                <div class="form-check-inline form-check">
                    @Html.RadioButtonFor(model => model.Assessment, "Excelente", new { id = "Excelente", @class = "form-check-input" })
                    @Html.Label("Excelente", "Excelente")

                    @Html.RadioButtonFor(model => model.Assessment, "Regular", new { id = "Regular", @class = "form-check-input" })
                    @Html.Label("Regular", "Regular")

                    @Html.RadioButtonFor(model => model.Assessment, "Deficiente", new { id = "Deficiente", @class = "form-check-input" })
                    @Html.Label("Deficiente", "Deficiente")
                </div>
            </div>
        </div>
        <hr>
        <div class="card-text text-sm-center">
            <button type="submit" class="btn btn-success btn-flat m-b-30 m-t-30">Siguiente</button>
        </div>
    </div>

} }

    
asked by Luis Acosta 09.04.2018 в 20:21
source

1 answer

1

I did something similar some time ago, look inside the Request for the file, it should work with something like this:

 [HttpPost]
public ActionResult Paso1(MiModelo modelo)
{
  // Validamos que este adjunto el archivo dentro de nuestro request y que no sea vacío..
  if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0)
  {
        // Obtenemos el archivo del request..
        var file = Request.Files[0];
        // Guardamos el archivo localmente..
        file.SaveAs("C:\AquiLaRuta");
        // Continua con tu lógica aquí..
  }
}

If you expect to receive more than one file, you must iterate Request.Files looking for them

I hope I could have helped you with my experience

Greetings

    
answered by 09.04.2018 в 21:41