Friday, May 04, 2007

How to download file from Server and How to Display PDF file in new browser using C#.net

How to download file from Server and How to Display PDF file in new browser using C#.net
NameSpace
using System.IO;
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);
//Or
FileInfo my_File = new FileInfo(file_Path);
Response.WriteFile(my_File.FullName);

Response.End();
}
catch (Exception Ex)
{
throw Ex;
}
}
Note: If pdf file size if big then above method will take time to display pdf in the browser; in that case we have to read pdf in bytes and flush or refersh while reding; use follwoing method in case of big pdf file

private void DisplayPDF(string filePath)
{

filePath = @"D:\All\CSC\CSC\Contents\wake1.pdf",

Response.ClearContent();
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();

}
Another way of opening Large PDF files, actually following method will read PDF file from server and write into the browser
Calling function to open PDF or any others files like: doc, wmv, xls etc. content type would be change according to file format like: for doc file content type would be "
application/msword" etc
How to use
viewDocument("C:\Temp\123.pdf","application/pdf")
Implementation
public void viewDocument(string filePath, string contentType)
{
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){
Response.BinaryWrite(arbData);
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();
}
}
Get the file size in MB/KB and Byte
private string GetFileSize(string filePath, ref double fileSizeMB, ref double fileSizeKB)
{
const int FILE_SIZE_MB = 1048576;
const int FILE_SIZE_KB = 1024;
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: