Hi alibasha,
Use FileResult to download the file.
Refer below sample. In this sample i am using fileupload control for uploading file and reading the byte array from it.
Then using the byte array save in folder and download it.
You need to change the byte array code with yours.
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
public FileResult Download(string legalid, HttpPostedFileBase postedFile)
{
byte[] bytes;
using (System.IO.Stream inputStream = postedFile.InputStream)
{
System.IO.MemoryStream memoryStream = inputStream as System.IO.MemoryStream;
if (memoryStream == null)
{
memoryStream = new System.IO.MemoryStream();
inputStream.CopyTo(memoryStream);
}
bytes = memoryStream.ToArray();
}
// Write file to server path.
WriteFile(legalid, bytes);
// Download the file.
return File(bytes, "application/pdf", legalid + "_" + "REDF Contract_" + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".pdf");
}
private void WriteFile(string nid, byte[] contractByte)
{
try
{
string savePath = Server.MapPath(@"~/SavePath/") + nid + "_" + "REDF Contract_" + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".pdf";
System.IO.File.WriteAllBytes(savePath, contractByte);
}
catch (Exception ex)
{
}
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@using (Html.BeginForm("Download", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="text" name="legalid" />
<input type="file" name="postedFile" />
<input type="submit" value="Download" />
}
</div>
</body>
</html>