0

"i want to send id and file data to same action uploadFile(int id, httpPostFileBase upFile) ?"

i tried to send the patient id via ajax during submit, and file using name attribute in input tag.

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

        <input type="file" name="upFile" />
        <br />
        <input type="submit" name="submit" value="Upload!" />
    }
var id = url.substring(url.lastIndexOf('/') + 1);

$("form").submit(function (e) {
                $.ajax({
                    type: "POST",
                    url: "@Url.Action("uploadFile","patientsProfile")",
                    data: {
                        Id: id
                    },
                    success: function (res) {
                        alert("id :" + id);
                    }
                })
            })
[HttpPost]
        public ActionResult uploadFile( HttpPostedFileBase upFile , int Id)
        {
            Tests tests = new Tests();

            tests.patients_Id = Id;

            string fileName = upFile.FileName;

            string UniquefileName = Guid.NewGuid() + Path.GetFileName(upFile.FileName);

            string filePath = Server.MapPath("~/Uploaded/");


            string actualPath = Path.Combine(filePath + UniquefileName);
            upFile.SaveAs(actualPath);

            tests.Name = fileName;
            tests.urlName = actualPath;
            db.Tests.Add(tests);
            db.SaveChanges();

            return RedirectToAction("index");
        }

httpPostFileBase upFile be null but id take it's value correctly

3 Answers3

0

First of all I think that the below code is going to cause issues

var id = url.substring(url.lastIndexOf('/') + 1);

Reasons

  • If it is used to get the id when the ids are only one character then it works well, but when the number of character increases it is going to get wrong values. example for a url www.yourdomain.com/controller/action/1 it will work fine, but www.yourdomain.com/controller/action/12 will return the same result as the first. So you might want to do some math there like (stringLength - lastIndexOf / )

I will suggest you use ViewBag to get that Id since it was passed from the GET method and then pass that to the form as a parameter like below

@using (Html.BeginForm("uploadFile", "Home", new{Id = ViewBag.Id}, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="upFile" id="upFile" />
    <br />
    <input type="submit" name="submit" value="Upload!" />
}

remember to include the view bag in the get request as

public ActionResult ActionName(int Id)
{
    ViewBag.Id = Id;
    return View();
}

IF you insist on using javascript then try including the file as a data like in this answer

Bosco
  • 1,536
  • 2
  • 13
  • 25
  • `var url = window.location.pathname; var id = url.substring(url.lastIndexOf('/') + 1);` this id get from path of the page, the main idea i need to upload file to this patient. in nedd to send patient_Id – Ahmed Shaaban Aug 17 '19 at 09:53
  • `[HttpPost] public ActionResult Index(string P_Id) { ViewBag.NestId = P_Id; return View(); }` – Ahmed Shaaban Aug 17 '19 at 11:08
  • I believe the `string P_Id` is the id you're referring to? That you want to send together with the form upload – Bosco Aug 17 '19 at 11:15
  • yes i understand you but this `ViewBag` doesn't work – Ahmed Shaaban Aug 17 '19 at 11:21
  • `function Profile(P_Id) { var url = "/patientsProfile/Index/" + P_Id; window.location.href = url; }` i use this to redirect to loading page . – Ahmed Shaaban Aug 17 '19 at 11:34
  • Okay. Then in your `Index` method of the `patientsProfile` controller, assign the ViewBag there. Because you are redirecting to it. The answer requires you to pass the viewbag as a route parameter together with the form here `Html.BeginForm("uploadFile", "Home", new{Id = ViewBag.NestId}, FormMethod.Post, new { enctype = "multipart/form-data" })`. It is actually a wrong idea to pick that if from JavaScript – Bosco Aug 17 '19 at 11:40
  • I think am getting more confused, `getPatientById()` is called with Ajax?. Can you explain the whole flow from when you want to load the initial page and then redirect to others to where you want to save the file – Bosco Aug 17 '19 at 11:45
0

you have to pass the upFile to to the data which you are sending.

data: {
    Id: id,
    upFile: //the file which you are sending
},
Bosco
  • 1,536
  • 2
  • 13
  • 25
0
       @using (Html.BeginForm("uploadFile", "Home", new{Id = ViewBag.Id}, 
       FormMethod.Post, new { enctype = "multipart/form-data" }))
       {
       <input type="file" name="upFile" id="upFile" />
       <input type="hidden" name="hdfid" id="hdfid" value ="" />
       <br />
       <input type="submit" name="submit" value="Upload!" />
       }


       var id = url.substring(url.lastIndexOf('/') + 1);

         $("#hdfid").val(id);
            $("form").submit(function (e) {
            $.ajax({
                type: "POST",
                url: "@Url.Action("uploadFile","patientsProfile")",

                success: function (res) {
                    alert("id :" + id);
                }
            })
        })







        public ActionResult uploadFile( formcollection form, HttpPostedFileBase 
        upFile )
        {
            Tests tests = new Tests();
            string id = form["id"];//or provide index
            tests.patients_Id = Id;

            string fileName = upFile.FileName;

            string UniquefileName = Guid.NewGuid() + 
            Path.GetFileName(upFile.FileName);

            string filePath = Server.MapPath("~/Uploaded/");


            string actualPath = Path.Combine(filePath + UniquefileName);
            upFile.SaveAs(actualPath);

            tests.Name = fileName;
            tests.urlName = actualPath;
            db.Tests.Add(tests);
            db.SaveChanges();

            return RedirectToAction("index");
        }
amaldec23
  • 245
  • 2
  • 11