Code On Controller to store file on server
[HttpPost]
public ActionResult UploadFiles()
{
int DocketId = 0;
int MyDoctId = 0;
string FinalUrl = "";
string sessVal = Session["UploadDoc"].ToString();
string[] words = sessVal.Split('#');
DocketId = Convert.ToInt32(words[0]);
MyDoctId = Convert.ToInt32(words[1]);
// Checking no of files injected in Request object
if (Request.Files.Count > 0)
{
try
{
// Get all files from Request object
HttpFileCollectionBase files = Request.Files;
for (int i = 0; i < files.Count; i++)
{
//string path = AppDomain.CurrentDomain.BaseDirectory + "Uploads/";
//string filename = Path.GetFileName(Request.Files[i].FileName);
HttpPostedFileBase file = files[i];
string fname;
// Checking for Internet Explorer
if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
{
string[] testfiles = file.FileName.Split(new char[] { '\\' });
fname = testfiles[testfiles.Length - 1];
}
else
{
fname = file.FileName;
}
fname = DocketId + "_" + MyDoctId + fname;
// Get the complete folder path and store the file inside it.
fname = Path.Combine(Server.MapPath("~/uploadsDoc/"), fname);
file.SaveAs(fname);
if (i == 0)
{
FinalUrl = fname;
}
else { FinalUrl = FinalUrl + "#" + fname; }
}
//Name of Table in edmx ST_UploadDocInfo
var result = (from u in dem.ST_UploadDocInfo
where u.MyDoctId == MyDoctId
&& u.DocketId == DocketId
select u).Single();
result.DocURL = FinalUrl;
dem.SaveChanges();
// Returns message that successfully uploaded
return Json("File Uploaded Successfully!");
}
catch (Exception ex)
{
return Json("Error occurred. Error details: " + ex.Message);
}
}
else
{
return Json("No files selected.");
}
}
[HttpPost]
public JsonResult GetImageDetails(int Id, string userName,string docType)
{
int DocketId=0;
int MyDoctId=0;
string DocUrl="";
if(docType!="" || docType!=null)
{
DocketId=Id;
MyDoctId=0;
}
Session["UploadDoc"] = DocketId + "#" + MyDoctId;
var result = dem.USP_UploadDocInfo(DocketId, MyDoctId, DocUrl, userName);
return Json(result, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public JsonResult UploadDocDetails(int Id, string docType)
{
int DocketId = 0;
int MyDoctId = 0;
if (docType != "" || docType != null)
{
DocketId = Id;
MyDoctId = 0;
}
var result = from details in dem.ST_UploadDocInfo
where details.MyDoctId == MyDoctId && details.DocketId == DocketId
select new
{
details.DocketId,
details.MyDoctId,
details.DocURL
};
var data = result.ToList();
string[] words = (data[0].DocURL.ToString()).Split('#');
var res =words.ToList();
return Json(res, JsonRequestBehavior.AllowGet);
}