I am trying to add file to my database but for some reason its not working can someone tell me why. This is what I've sofa:
$(document).on("click", "#BUTTON", function () {
var FILE= $('#name').val();
$.ajax({
type: "POST",
dataType: 'json',
url: "@Url.Action("File", "Controller")",
data: JSON.stringify(FILE),
contentType: "application/json; charset=utf-8",
success: function () {
}
});
});
This is what happens in my controller:
public ActionResult File(HttpPostedFileBase file)
{
if (file != null)
{
string pic = System.IO.Path.GetFileName(file.FileName);
string path = System.IO.Path.Combine(Server.MapPath("~/path"), pic);
// file is uploaded
file.SaveAs(path);
// save the image path path to the database or you can send image
// directly to database
// in-case if you want to store byte[] ie. for DB
using (MemoryStream ms = new MemoryStream())
{
file.InputStream.CopyTo(ms);
byte[] array = ms.GetBuffer();
}
}
// after successfully uploading redirect the user
return Index();
}
}
When I place a brakepoint in line HttpPostedFileBase file
then file has null value I dont know why? If i do alert(FILE);
then I am getting full file path e.g. G:/filename/imagename.jpg but in controller the path is no being passed?