I am using the following code to upload documents. I am using ajax in this scenario.
In the following code i get Request.Files.Count
as 0. Therefore the file doesn't get uploaded.
Front-end code:
<input type="file" name="name" id="pd-fileName" />
....
<button class="btn pr-btn" id="pd-addBtn" type="button" onclick="insertDocument()">Add</button>
function insertDocument() {
......
jq.post('/Main/addDocument?Id=' + Id , function (data) {
alert('Data saved successfully.!');
......
});
});
}
Code in the controller:
[HttpPost]
public JsonResult addDocument(int Id)
{
string pathPhysical = Server.MapPath("~/Documents/" + Id + "/");
if (!Directory.Exists(pathPhysical))
{
DirectoryInfo di = Directory.CreateDirectory(pathPhysical);
}
if (Request.Files.Count > 0)
{
var file = Request.Files[0];
if (file != null && file.ContentLength > 0)
{
var documentName = Path.GetFileName(file.FileName);
pathPhysical = Path.Combine(pathPhysical, documentName);
file.SaveAs(pathPhysical);
}
}
return Json(JsonRequestBehavior.AllowGet);
}
How do I solve this?