How to download file from Server and How to Display PDF file in new browser using C#.net
1) Download files from file server into client machine with dialog box (Save/Open)
private void StartDownLoad(string filePath)
{
try
{
filePath = "C:\backup\" + filePath;
//to download the file
FileInfo myFile = new FileInfo(filePath);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + myFile.Name );
Response.AddHeader("Content-Length", myFile.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(myFile.FullName);
Response.Flush();
Response.End();
}
catch (Exception Ex)
{
throw Ex;
}
}
2) Display PDF document into new web browser without dialog box
(below methods works fine with Acrobat Reader 5.0)
private void DisplayPDF(string filePath)
{
try
{
string file_Path = @"C:\backup\standards.pdf";
Response.Buffer = false; //transmitfile self buffers
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
//transmitfile keeps entire file from loading into memory
Response.TransmitFile(file_Path);
Response.TransmitFile(file_Path);
//Or
FileInfo my_File = new FileInfo(file_Path);
Response.WriteFile(my_File.FullName);
FileInfo my_File = new FileInfo(file_Path);
Response.WriteFile(my_File.FullName);
Response.End();
}
catch (Exception Ex)
{
throw Ex;
}
}
{
Response.ContentType = "application/pdf";
//Get the File path from product Library Tab
FileStream oStrm = new FileStream(filePath,FileMode.Open, FileAccess.Read);
byte[] arbData = new byte[10000];
int nC = 0;
//Conert PDF file to byte
while (0 != (nC = oStrm.Read(arbData, 0, 10000)))
{
Response.OutputStream.Write(arbData, 0, nC);
Response.OutputStream.Flush();
Array.Clear(arbData, 0, nC);
Response.Flush();
}
Response.End();
application/msword" etc
Implementation
{
if (contentType.Contains("pdf"))
{
Int32 bufferInByte = 2500;
Response.ClearHeaders();
Response.ClearContent();
Response.Clear();
Response.ContentType = contentType;
Response.BufferOutput = true;
Response.Buffer = true;
Response.AppendHeader("Accept-Ranges", "bytes");
Response.StatusCode = 200;
Response.AppendHeader("Content-Type", contentType);
FileStream oStrm = new FileStream(filePath, FileMode.Open, FileAccess.Read);
long lEndPos = oStrm.Length;
Response.AppendHeader("Content-Length", oStrm.Length.ToString());
Response.AppendHeader("Accept-Header", oStrm.Length.ToString());
Response.AppendHeader("connection", "close");
if (Request.HttpMethod.Equals("HEAD"))
{
Response.Flush();
return;
}
byte[] arbData = new byte[bufferInByte];
int nC = 0;
//Conert PDF file to byte
while (0 != (nC = oStrm.Read(arbData, 0, bufferInByte)))
{
if (nC <>(ref arbData, nC); }
if (Response.IsClientConnected){
Array.Clear(arbData, 0, nC);
Response.Flush();
if (nC <>(ref arbData, bufferInByte); }
}
else {
break;
}
oStrm.Close();
Response.Flush();
}
else
{
//to download others file
FileInfo myFile = new FileInfo(filePath);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + myFile.Name);
Response.AddHeader("Content-Length", myFile.Length.ToString());
Response.ContentType = ConfigurationManager.AppSettings["DefaultContentType"].ToString();
Response.WriteFile(myFile.FullName);
Response.Flush();
Response.End();
}
}
private string GetFileSize(string filePath, ref double fileSizeMB, ref double fileSizeKB)
{
lblError.Text = string.Empty;
double fileSize = 0;
if (filePath.Length > 0)
{
FileStream FSIn = new FileStream(filePath.ToString(), FileMode.Open
, FileAccess.Read);
BinaryReader rFSIn = new BinaryReader(FSIn);
fileSize = FSIn.Length;
fileSize = Math.Round(fileSize, 2);
FSIn.Close();
rFSIn.Close();
fileSizeMB = fileSize / FILE_SIZE_MB;;
if (Math.Round(fileSizeMB,1) == 0.0)
{
fileSizeKB = fileSize / FILE_SIZE_KB;
fileSizeKB = Math.Round(fileSizeKB, 2);
if (fileSizeKB == 0)
return fileSize + " Byte";
else
return fileSizeKB + " KB";
}
else
{
fileSizeMB = Math.Round(fileSizeMB,2);
return fileSizeMB + " MB";
}
}
else
{
lblError.Text = "FILE_NOT_FOUND Error";
return fileSize + "";
}
}
No comments:
Post a Comment