10
Answers

How to Download in in file

barathi raja

barathi raja

8y
3.5k
1
Download for pdf,documents,and video and audio use c# use in my project use download any file ...
Answers (10)
2
Rajesh Gami

Rajesh Gami

77 24.4k 1.3m 7y
try this code for download pdf file.
 
  1. #region Export PDF  
  2.        public async Task<FileContentResult> ExportPDF()  
  3.        {  
  4.            //student list  
  5.            //var dataList = GetStudentList();  
  6.            var users = _context.TEntity.Select(u => new Employee  
  7.            {  
  8.                Id = u.Id,  
  9.                Name = u.Name,  
  10.                Address = u.Address,  
  11.                Birthdate = u.Birthdate,  
  12.                Gender = u.Gender,  
  13.                Email = u.Email,  
  14.                Designation = u.Designation,  
  15.                Salary = u.Salary  
  16.   
  17.   
  18.            }).ToList();  
  19.   
  20.            if (users == nullreturn null;  
  21.            //column Header name  
  22.            var columnsHeader = new List<string>{  
  23.                "S/N",  
  24.                "Name",  
  25.                "Address",  
  26.                "Birthdate",  
  27.                "Gender",  
  28.                "Email",  
  29.                "Designation",  
  30.                "Salary"  
  31.            };  
  32.            var filecontent = ExportPDF(users, columnsHeader, "TEntity");  
  33.            return File(filecontent, "application/pdf""Employee.pdf"); ;  
  34.        }  
  35.        #endregion  
  36.        #region Helpers of Export PDF  
  37.        private byte[] ExportPDF(List<Employee> dataList, List<string> columnsHeader, string heading)  
  38.        {  
  39.   
  40.            var document = new Document();  
  41.            var outputMS = new MemoryStream();  
  42.            var writer = PdfWriter.GetInstance(document, outputMS);  
  43.            document.Open();  
  44.            var font5 = FontFactory.GetFont(FontFactory.HELVETICA, 11);  
  45.   
  46.            document.Add(new Phrase(Environment.NewLine));  
  47.   
  48.            //var count = typeof(UserListVM).GetProperties().Count();  
  49.            var count = columnsHeader.Count;  
  50.            var table = new PdfPTable(count);  
  51.            float[] widths = new float[] { 2f, 5f, 5f, 5f, 3f, 6f, 4f, 3f };  
  52.   
  53.            table.SetWidths(widths);  
  54.   
  55.            table.WidthPercentage = 100;  
  56.            var cell = new PdfPCell(new Phrase(heading));  
  57.            cell.Colspan = count;  
  58.   
  59.            for (int i = 0; i < count; i++)  
  60.            {  
  61.                var headerCell = new PdfPCell(new Phrase(columnsHeader[i], font5));  
  62.                headerCell.BackgroundColor = BaseColor.Gray;  
  63.                table.AddCell(headerCell);  
  64.            }  
  65.   
  66.            var sn = 1;  
  67.            foreach (var item in dataList)  
  68.            {  
  69.                table.AddCell(new Phrase(sn.ToString(), font5));  
  70.                table.AddCell(new Phrase(item.Name, font5));  
  71.                table.AddCell(new Phrase(item.Address, font5));  
  72.                table.AddCell(new Phrase(item.Birthdate.ToString(), font5));  
  73.                table.AddCell(new Phrase(item.Gender, font5));  
  74.                table.AddCell(new Phrase(item.Email, font5));  
  75.                table.AddCell(new Phrase(item.Designation, font5));  
  76.                table.AddCell(new Phrase(item.Salary.ToString(), font5));  
  77.   
  78.                sn++;  
  79.            }  
  80.   
  81.            document.Add(table);  
  82.            document.Close();  
  83.            var result = outputMS.ToArray();  
  84.   
  85.            return result;  
  86.        }  
  87.  
  88.        #endregion 
 step 2: Now in list view
 
  1. @model IEnumerable<ProjectName.Models.Employee>  
  2.   
  3. @{  
  4.     ViewData["Title"] = "Index";  
  5. }  
  6.   
  7. <h2>Index</h2>  
  8. <script src="~/js/pdf.js"></script>  
  9. <p>  
  10.     <a class="btn btn-default" asp-action="Create">Create New</a> 

  11.     <a class="btn btn-default" style="float:right;" asp-action="ExportPDF">Create PDF</a>  
  12.   
  13. </p>  
  14. <table class="table">  
  15.     <thead>  
  16.         <tr>  
  17.                 <th>  
  18.                     @Html.DisplayNameFor(model => model.Id)  
  19.                 </th>  
  20.                 <th>  
  21.                     @Html.DisplayNameFor(model => model.Name)  
  22.                 </th>  
  23.                 <th>  
  24.                     @Html.DisplayNameFor(model => model.Address)  
  25.                 </th>  
  26.                 <th>  
  27.                     @Html.DisplayNameFor(model => model.Birthdate)  
  28.                 </th>  
  29.                 <th>  
  30.                     @Html.DisplayNameFor(model => model.Gender)  
  31.                 </th>  
  32.                 <th>  
  33.                     @Html.DisplayNameFor(model => model.Email)  
  34.                 </th>  
  35.                 <th>  
  36.                     @Html.DisplayNameFor(model => model.Designation)  
  37.                 </th>  
  38.                 <th>  
  39.                     @Html.DisplayNameFor(model => model.Salary)  
  40.                 </th>  
  41.                 <th>  
  42.                     @Html.DisplayNameFor(model => model.Resume)  
  43.                 </th>  
  44.                 <th>  
  45.                     @Html.DisplayNameFor(model => model.Photo)  
  46.                 </th>  
  47.             <th></th>  
  48.         </tr>  
  49.     </thead>  
  50.     <tbody>  
  51. @foreach (var item in Model) {  
  52.         <tr>  
  53.             <td>  
  54.                 @Html.DisplayFor(modelItem => item.Id)  
  55.             </td>  
  56.             <td>  
  57.                 @Html.DisplayFor(modelItem => item.Name)  
  58.             </td>  
  59.             <td>  
  60.                 @Html.DisplayFor(modelItem => item.Address)  
  61.             </td>  
  62.             <td>  
  63.                 @Html.DisplayFor(modelItem => item.Birthdate)  
  64.             </td>  
  65.             <td>  
  66.                 @Html.DisplayFor(modelItem => item.Gender)  
  67.             </td>  
  68.             <td>  
  69.                 @Html.DisplayFor(modelItem => item.Email)  
  70.             </td>  
  71.             <td>  
  72.                 @Html.DisplayFor(modelItem => item.Designation)  
  73.             </td>  
  74.             <td>  
  75.                 @Html.DisplayFor(modelItem => item.Salary)  
  76.             </td>  
  77.             <td>  
  78.                 @Html.DisplayFor(modelItem => item.Resume)  
  79.             </td>  
  80.             <td>  
  81.                 @Html.DisplayFor(modelItem => item.Photo)  
  82.             </td>  
  83.             <td>  
  84.                 @Html.ActionLink("Edit""Edit"new { id=item.Id }) |  
  85.                 @Html.ActionLink("Details""Details"new { id = item.Id }) |  
  86.                 @Html.ActionLink("Delete""Delete"new { id = item.Id })  
  87.             </td>  
  88.         </tr>  
  89. }  
  90.     </tbody>  
  91. </table> 
 thanx
1
Rajeesh Menoth

Rajeesh Menoth

66 27.1k 2.7m 8y
Hi,
Try this code :
  1. protectedvoidlnkfilepath_Click(objectsender,EventArgse)//urlinkbutton
  2. {
  3. stringfilename=lnkfilepath.Text;
  4. stringFilpath=Server.MapPath(“~/Attachments/”+filename);
  5. DownLoad(Filpath);
  6. }
  7. publicvoidDownLoad(stringFName)
  8. {
  9. stringpath=FName;
  10. System.IO.FileInfofile=newSystem.IO.FileInfo(path);
  11. if(file.Exists)
  12. {
  13. Response.Clear();
  14. Response.AddHeader(“Content-Disposition”,“attachment;filename=”+file.Name);
  15. Response.AddHeader(“Content-Length”,file.Length.ToString());
  16. Response.ContentType=“application/octet-stream”;//downloadalltypesoffiles..
  17. Response.WriteFile(file.FullName);
  18. Response.End();
  19. }
  20. else
  21. {
  22. Response.Write(“Thisfiledoesnotexist.”);
  23. }
  24. }
Reference :
https://rajeeshmenoth.wordpress.com/2014/06/25/how-to-download-files-using-asp-net-c/
1
Nilesh Sawardekar

Nilesh Sawardekar

399 3.9k 400.2k 8y
try this
http://www.csharp-examples.net/download-files/
0
Rajesh Gami

Rajesh Gami

77 24.4k 1.3m 7y
Hello barathi
 
please refer this code for Download Video.
  1. //Create a stream for the file  
  2.     Stream stream = null;  
  3.    
  4.     //This controls how many bytes to read at a time and send to the client  
  5.     int bytesToRead = 10000;  
  6.    
  7.     // Buffer to read bytes in chunk size specified above  
  8.     byte[] buffer = new Byte[bytesToRead];  
  9.    
  10.     // The number of bytes read  
  11.     try  
  12.     {  
  13.       //Create a WebRequest to get the file  
  14.       HttpWebRequest fileReq = (HttpWebRequest) HttpWebRequest.Create(url);  
  15.    
  16.       //Create a response for this request  
  17.       HttpWebResponse fileResp = (HttpWebResponse) fileReq.GetResponse();  
  18.    
  19.       if (fileReq.ContentLength > 0)  
  20.         fileResp.ContentLength = fileReq.ContentLength;  
  21.    
  22.         //Get the Stream returned from the response  
  23.         stream = fileResp.GetResponseStream();  
  24.    
  25.         // prepare the response to the client. resp is the client Response  
  26.         var resp = HttpContext.Current.Response;  
  27.    
  28.         //Indicate the type of data being sent  
  29.         resp.ContentType = "application/octet-stream";  
  30.    
  31.         //Name the file  
  32.         resp.AddHeader("Content-Disposition""attachment; filename=\"" + fileName + "\"");  
  33.         resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());  
  34.    
  35.         int length;  
  36.         do  
  37.         {  
  38.             // Verify that the client is connected.  
  39.             if (resp.IsClientConnected)  
  40.             {  
  41.                 // Read data into the buffer.  
  42.                 length = stream.Read(buffer, 0, bytesToRead);  
  43.    
  44.                 // and write it out to the response's output stream  
  45.                 resp.OutputStream.Write(buffer, 0, length);  
  46.    
  47.                 // Flush the data  
  48.                 resp.Flush();  
  49.    
  50.                 //Clear the buffer  
  51.                 buffer = new Byte[bytesToRead];  
  52.             }  
  53.             else  
  54.             {  
  55.                 // cancel the download if client has disconnected  
  56.                 length = -1;  
  57.             }  
  58.         } while (length > 0); //Repeat until no data is read  
  59.     }  
  60.     finally  
  61.     {  
  62.         if (stream != null)  
  63.         {  
  64.             //Close the input stream  
  65.             stream.Close();  
  66.         }  
  67.     } 
 
0
Rafnas T P

Rafnas T P

141 13.4k 4.5m 7y
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
  1. using (var client = new WebClient())  
  2. {  
  3.     client.DownloadFile("http://example.com/file/song/a.pdf""a.pdf");//download pdf  

 
 
0
Jyothi Augustine

Jyothi Augustine

NA 75 2.7k 7y
error in this line
 
Response.Clear();
0
Jyothi Augustine

Jyothi Augustine

NA 75 2.7k 7y
i want to download any file from folder dotnet windows appilication using c#
user02.futuro@gmail.com send me plz
0
Munesh Sharma

Munesh Sharma

105 17.2k 6.2m 8y
http://www.aspdotnet-suresh.com/2012/02/saveupload-files-in-folder-and-download.html
0
Manoj Bhoir

Manoj Bhoir

287 6.6k 612.3k 8y
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
Nitin

Nitin

311 6k 5.1m 8y
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)
{
}
}