[HttpGet]
public ActionResult DownloadDocument(string fileName)
{
try
{
var filePath = GetImportsPath();
var newFileFullPath = Path.Combine(filePath, fileName);
var fileExtension = Path.GetExtension(fileName);
var reConstructedFileName = "ConstituentId" + "_" + DateTime.UtcNow.ToShortDateString() + "_" + "OriginalFileName" + fileExtension;
if (fileExtension == null)
throw new PersistenceValidationException("Export Error", new List<BrokenRule>
{
new BrokenRule("InvalidDownload", "The file you are looking for download does not exist.")
});
if (fileExtension.ToLower() == ".zip" || fileExtension.ToLower() == ".rar")
{
return File(newFileFullPath, "application/zip", reConstructedFileName);
}
return File(newFileFullPath, "application/octet-stream", reConstructedFileName);
}
catch (PersistenceValidationException ex)
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(ex.BrokenRules, JsonRequestBehavior.AllowGet);
}
}
_________________________________________
protected static string GetImportsPath()
{
var fileUploadLocation = ConfigurationManager.AppSettings["importsPath"];
if (!Directory.Exists(fileUploadLocation))
Directory.CreateDirectory(fileUploadLocation);
return fileUploadLocation;
}
___________________________
write below code in your webconfig file under appsetting
<appSettings>
<add key="importsPath" value="C:\LocalFiles\" />
</appSettings
public ActionResult DownloadDocument(string fileName)
{
try
{
var filePath = GetImportsPath();
var newFileFullPath = Path.Combine(filePath, fileName);
var fileExtension = Path.GetExtension(fileName);
var reConstructedFileName = "ConstituentId" + "_" + DateTime.UtcNow.ToShortDateString() + "_" + "OriginalFileName" + fileExtension;
if (fileExtension == null)
throw new PersistenceValidationException("Export Error", new List<BrokenRule>
{
new BrokenRule("InvalidDownload", "The file you are looking for download does not exist.")
});
if (fileExtension.ToLower() == ".zip" || fileExtension.ToLower() == ".rar")
{
return File(newFileFullPath, "application/zip", reConstructedFileName);
}
return File(newFileFullPath, "application/octet-stream", reConstructedFileName);
}
catch (PersistenceValidationException ex)
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(ex.BrokenRules, JsonRequestBehavior.AllowGet);
}
}
_________________________________________
protected static string GetImportsPath()
{
var fileUploadLocation = ConfigurationManager.AppSettings["importsPath"];
if (!Directory.Exists(fileUploadLocation))
Directory.CreateDirectory(fileUploadLocation);
return fileUploadLocation;
}
___________________________
write below code in your webconfig file under appsetting
<appSettings>
<add key="importsPath" value="C:\LocalFiles\" />
</appSettings
No comments:
Post a Comment