"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