How to Download a file from server

Posted by Abhisekjani under C# on 10/9/2012 | Points: 10 | Views : 2981 | Status : [Member] | Replies : 4
Hi...

I want to download a file from server when i click a link/button the file should download or it ask for save open cancle


Thanks

Abhisek




Responses

Posted by: Kanje_Ravi on: 10/9/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Abhisekjani,

by keeping in assumption that your file is in WEBSERVER. Please find the below function and call this function in button click .let me know if you need any further help.



VB.NET
====================================================================
Function DownloadFile(ByVal FileName As String)

Dim path As String = Server.MapPath("/Attachments/") + FileName
If System.IO.File.Exists(path) = True Then
Dim file As System.IO.FileInfo = New System.IO.FileInfo(path)
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()
End If
End Function
====================================================================
C SHARP CODE
====================================================================
public object DownloadFile(string FileName)
{

string path = Server.MapPath("/Attachments/") + FileName;
if (System.IO.File.Exists(path) == true) {
System.IO.FileInfo file = new System.IO.FileInfo(path);
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();
}
}

Thanks & Regards,
Ravindra M
(Success Always Depends on Dedication & Commitment)

Abhisekjani, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhisekjani on: 10/9/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Ravindra M

its throw the following error
Response.WriteFile(file.FullName);
in line

The filename, directory name, or volume label syntax is incorrect.

Abhisekjani, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vivekjj on: 10/9/2012 [Member] Starter | Points: 25

Up
0
Down
FileInfo file = new FileInfo("Your FilePath");

Response.ClearContent();
Response.AddHeader("Content-Disposition", "inline;filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/msword";
Response.TransmitFile(file.FullName);
Response.End();


Abhisekjani, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dg.Thiru on: 10/9/2012 [Member] Starter | Points: 25

Up
0
Down
Just Copy and Paste the following code in your button click event and Modify as ur like.
-------------------------------------------------------------------------------------------------------------------------------

Dim fs As System.IO.FileStream = Nothing
fs = System.IO.File.Open("Here Ur File Path", System.IO.FileMode.Open)
btFile = New Byte(fs.Length - 1) {}
fs.Read(btFile, 0, Convert.ToInt32(fs.Length))
fs.Close()
Response.Clear()
Response.AppendHeader("Content-disposition", "attachment; filename=" & Here Ur File name)
Response.ContentType = contentType
context.Response.BinaryWrite(btFile)
Response.Flush()




 Download source file

Abhisekjani, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response