2
try this code for download pdf file.
- #region Export PDF
- public async Task<FileContentResult> ExportPDF()
- {
-
-
- var users = _context.TEntity.Select(u => new Employee
- {
- Id = u.Id,
- Name = u.Name,
- Address = u.Address,
- Birthdate = u.Birthdate,
- Gender = u.Gender,
- Email = u.Email,
- Designation = u.Designation,
- Salary = u.Salary
-
-
- }).ToList();
-
- if (users == null) return null;
-
- var columnsHeader = new List<string>{
- "S/N",
- "Name",
- "Address",
- "Birthdate",
- "Gender",
- "Email",
- "Designation",
- "Salary"
- };
- var filecontent = ExportPDF(users, columnsHeader, "TEntity");
- return File(filecontent, "application/pdf", "Employee.pdf"); ;
- }
- #endregion
- #region Helpers of Export PDF
- private byte[] ExportPDF(List<Employee> dataList, List<string> columnsHeader, string heading)
- {
-
- var document = new Document();
- var outputMS = new MemoryStream();
- var writer = PdfWriter.GetInstance(document, outputMS);
- document.Open();
- var font5 = FontFactory.GetFont(FontFactory.HELVETICA, 11);
-
- document.Add(new Phrase(Environment.NewLine));
-
-
- var count = columnsHeader.Count;
- var table = new PdfPTable(count);
- float[] widths = new float[] { 2f, 5f, 5f, 5f, 3f, 6f, 4f, 3f };
-
- table.SetWidths(widths);
-
- table.WidthPercentage = 100;
- var cell = new PdfPCell(new Phrase(heading));
- cell.Colspan = count;
-
- for (int i = 0; i < count; i++)
- {
- var headerCell = new PdfPCell(new Phrase(columnsHeader[i], font5));
- headerCell.BackgroundColor = BaseColor.Gray;
- table.AddCell(headerCell);
- }
-
- var sn = 1;
- foreach (var item in dataList)
- {
- table.AddCell(new Phrase(sn.ToString(), font5));
- table.AddCell(new Phrase(item.Name, font5));
- table.AddCell(new Phrase(item.Address, font5));
- table.AddCell(new Phrase(item.Birthdate.ToString(), font5));
- table.AddCell(new Phrase(item.Gender, font5));
- table.AddCell(new Phrase(item.Email, font5));
- table.AddCell(new Phrase(item.Designation, font5));
- table.AddCell(new Phrase(item.Salary.ToString(), font5));
-
- sn++;
- }
-
- document.Add(table);
- document.Close();
- var result = outputMS.ToArray();
-
- return result;
- }
-
- #endregion
step 2: Now in list view
- @model IEnumerable<ProjectName.Models.Employee>
-
- @{
- ViewData["Title"] = "Index";
- }
-
- <h2>Index</h2>
- <script src="~/js/pdf.js"></script>
- <p>
- <a class="btn btn-default" asp-action="Create">Create New</a>
- <a class="btn btn-default" style="float:right;" asp-action="ExportPDF">Create PDF</a>
-
- </p>
- <table class="table">
- <thead>
- <tr>
- <th>
- @Html.DisplayNameFor(model => model.Id)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Name)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Address)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Birthdate)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Gender)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Email)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Designation)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Salary)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Resume)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Photo)
- </th>
- <th></th>
- </tr>
- </thead>
- <tbody>
- @foreach (var item in Model) {
- <tr>
- <td>
- @Html.DisplayFor(modelItem => item.Id)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Name)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Address)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Birthdate)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Gender)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Email)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Designation)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Salary)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Resume)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Photo)
- </td>
- <td>
- @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
- @Html.ActionLink("Details", "Details", new { id = item.Id }) |
- @Html.ActionLink("Delete", "Delete", new { id = item.Id })
- </td>
- </tr>
- }
- </tbody>
- </table>
thanx

1
Hi,
Try this code :
- protectedvoidlnkfilepath_Click(objectsender,EventArgse)
- {
- stringfilename=lnkfilepath.Text;
- stringFilpath=Server.MapPath(“~/Attachments/”+filename);
- DownLoad(Filpath);
- }
- publicvoidDownLoad(stringFName)
- {
- stringpath=FName;
- System.IO.FileInfofile=newSystem.IO.FileInfo(path);
- if(file.Exists)
- {
- Response.Clear();
- Response.AddHeader(“Content-Disposition”,“attachment;filename=”+file.Name);
- Response.AddHeader(“Content-Length”,file.Length.ToString());
- Response.ContentType=“application/octet-stream”;
- Response.WriteFile(file.FullName);
- Response.End();
- }
- else
- {
- Response.Write(“Thisfiledoesnotexist.”);
- }
- }
Reference :https://rajeeshmenoth.wordpress.com/2014/06/25/how-to-download-files-using-asp-net-c/
1
try this
http://www.csharp-examples.net/download-files/
0
Hello barathi
please refer this code for Download Video.
-
- Stream stream = null;
-
-
- int bytesToRead = 10000;
-
-
- byte[] buffer = new Byte[bytesToRead];
-
-
- try
- {
-
- HttpWebRequest fileReq = (HttpWebRequest) HttpWebRequest.Create(url);
-
-
- HttpWebResponse fileResp = (HttpWebResponse) fileReq.GetResponse();
-
- if (fileReq.ContentLength > 0)
- fileResp.ContentLength = fileReq.ContentLength;
-
-
- stream = fileResp.GetResponseStream();
-
-
- var resp = HttpContext.Current.Response;
-
-
- resp.ContentType = "application/octet-stream";
-
-
- resp.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
- resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());
-
- int length;
- do
- {
-
- if (resp.IsClientConnected)
- {
-
- length = stream.Read(buffer, 0, bytesToRead);
-
-
- resp.OutputStream.Write(buffer, 0, length);
-
-
- resp.Flush();
-
-
- buffer = new Byte[bytesToRead];
- }
- else
- {
-
- length = -1;
- }
- } while (length > 0);
- }
- finally
- {
- if (stream != null)
- {
-
- stream.Close();
- }
- }
0
hi,
you can download file giving url
try below code
code will dowload file from given url and in red color you have to mention filename with extention
- using (var client = new WebClient())
- {
- client.DownloadFile("http://example.com/file/song/a.pdf", "a.pdf");//download pdf
- }
0
i want to download any file from folder dotnet windows appilication using c#
user02.futuro@gmail.com send me plz
0
http://www.aspdotnet-suresh.com/2012/02/saveupload-files-in-folder-and-download.html
0
Hi,
You can write code like this :
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "APPLICATION/OCTET-STREAM";
response.AddHeader("Content-Disposition", "attachment; filename=" + yourfileName + ";");
response.TransmitFile(Server.MapPath("FileName"));
response.Flush();
response.End();
Check this links for how to implement file download.
http://www.aspdotnet-suresh.com/2012/02/saveupload-files-in-folder-and-download.html
http://www.codeproject.com/Tips/185886/File-Upload-and-Download-in-ASP-NET
http://stackoverflow.com/questions/18477398/asp-net-file-download-from-server
0
Use the following Code
protected void Download_Click(object sender, EventArgs e)
{
try
{
string strURL = txtFileName.Text;
WebClient req = new WebClient();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
response.AddHeader("Content-Disposition", "attachment;filename=\"" + Server.MapPath(strURL) + "\"");
byte[] data = req.DownloadData(Server.MapPath(strURL));
response.BinaryWrite(data);
response.End();
}
catch (Exception ex)
{
}
}